# 康复训练3

UPD 7.24:留一道UVa212……溜了溜了

UVa12096

//将s1,s2合并到s中
set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s, s.begin()));
//将s1,s2取交赋给s
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s, s.begin()));

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

stack<int> S;
map<set<int>, int> ID;
vector<set<int> > Set;

void Init() {
while (!S.empty()) S.pop();
ID.clear();
Set.clear();
}

int GetID(set<int> A) {
if (ID.count(A)) RT ID[A];
Set.pb(A);
RT ID[A] = Set.size() - 1;
}

void Main() {
int T;
cin >> T;
while (T --) {
Init();

int n;
cin >> n;
while (n --) {
string opt;
cin >> opt;

if (opt[0] == 'P') S.push(GetID(set<int>()));
else if (opt[0] == 'D') S.push(S.top());
else {
set<int> A, B, C;
A = Set[S.top()];
S.pop();
B = Set[S.top()];
S.pop();

switch (opt[0]) {
case 'U':
set_union(ALL(A), ALL(B), inserter(C, C.begin()));
BK;

case 'I':
set_intersection(ALL(A), ALL(B), inserter(C, C.begin()));
BK;

case 'A':
C = B;
C.insert(GetID(A));
BK;
}

S.push(GetID(C));
}
cout << Set[S.top()].size() << endl;
}
cout << "***" << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa1592

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

MSI id;
int cnt;
int n, m;
string dat[10005][11];

void Read(int r) {
string str;
getline(cin, str);

int s = 0, t = 0;
FOR (i, 1, m) { //c
while (t < str.size()) {
if (str[t] != ',') ++ t; else BK;
}
dat[r][i] = str.substr(s, t - s);
s = (++ t);
}
}

void Main() {
while (cin >> n >> m) {
getchar();
FOR (i, 1, n) Read(i);
/*
FOR (i, 1, n) {
FOR (j, 1, m) {
cout << dat[i][j] << '*';
}
cout << endl;
}
*/

bool flag = true;
FOR (c1, 1, m - 1) FOR (c2, c1 + 1, m) {
if (!flag) BK;
cnt = 0;
id.clear();

FOR (r, 1, n) {
string t = dat[r][c1] + "," + dat[r][c2];
//cout << t << endl;
if (!id.count(t)) id[t] = r;
else {
flag = false;
cout << "NO" << endl;
cout << id[t] << ' ' << r << endl;
cout << c1 << ' ' << c2 << endl;
BK;
}
}
}

if (flag) cout << "YES" << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa207

1）所有选手先进行前两轮，除非犯规（DQ）

2）所有犯规的选手当场出局，前两轮犯规的选手不得晋级，后两轮犯规的选手不能赢得奖金

3）前两轮结束的时候，选出70个得分相加最低的选手晋级，如果第70名有并列也一并选入，没有晋级的选手就不能继续后两轮和赢得奖金

4）晋级的选手再打两轮比赛，并获得总奖金的一定比例

5）选手获得总奖金的比例由排名决定

6）本场巡回赛中只有一个冠军

7）如果第k名有n个人并列，则第k~n-k+1名的奖金相加平均分给n个人，奖金精确到小数点后两位

8）如果犯规使得选手少于70人了，为剩下的名次准备的奖金不予分配

9）业余选手可以参赛但无法获得奖金，奖金分配跳过业余选手按照名次顺延

10）只有前70名职业选手（包括并列）能够获得奖金

1）输入输出格式题目中规定精确到了占位长度，所以注意使用相应的格式符

2）带*的选手是业余选手

3）输出时名次后带的T代表并列，但是业余选手不该有此标记！

4）在前两轮出局的选手不会被输出

5）并列选手按照字典序输出

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

const int DQ = 500000000;

struct Player {
string name, place;
int rd[5];
int total2, total4;
DB money;
bool amateur;
};

vector<Player> p;

bool Cmp2(Player A, Player B) {
RT A.total2 == B.total2 ? A.name < B.name : A.total2 < B.total2;
}

bool Cmp4(Player A, Player B) {
RT A.total4 == B.total4 ? A.name < B.name : A.total4 < B.total4;
}

int n;
DB money, won[100];

int Strtonum(string str) {
int res = 0;
FOR (i, 0, str.size() - 1) if (isdigit(str[i])) res = res * 10 + str[i] - '0';
RT res;
}

string Numtostr(int num) {
ostringstream ss;
ss << num;
RT ss.str();
}

void Scan() {
//prize
cin >> money;
FOR (i, 1, 70) {
cin >> won[i];
won[i] = won[i] / 100.0 * money;
}

(cin >> n).get();
p.resize(n);

//player
FOR (i, 0, n - 1) {
string str;
getline(cin, str);
//name
p[i].name = str.substr(0, 20);

//amateur
p[i].amateur = false;
ROF (j, 19, 0)
if (p[i].name[j] == '*') {
p[i].amateur = true;
BK;
}

istringstream ss(str.substr(20, str.size() - 20));

//round
FOR (j, 1, 4) p[i].rd[j] = DQ;
FOR (j, 1, 4) {
ss >> str;
if (str == "DQ") BK;
p[i].rd[j] = Strtonum(str);
}

//total score
p[i].total2 = p[i].total4 = 0;
FOR (j, 1, 2) p[i].total2 += p[i].rd[j];
FOR (j, 1, 4) p[i].total4 += p[i].rd[j];
}
}

void Solve() {
//round2
sort(p.begin(), p.end(), Cmp2);
FOR (i, 69, n - 1) if (i == n - 1 || p[i].total2 != p[i + 1].total2) { n = i + 1; BK; }

//round4
p.resize(n);
sort(p.begin(), p.end(), Cmp4);

printf("Player Name          Place     RD1  RD2  RD3  RD4  TOTAL     Money Won\n");
printf("-----------------------------------------------------------------------\n");

int pos = 1;
FOR (i, 0, n - 1) {
if (p[i].total4 >= DQ) { //DQ!
printf("%s           ", p[i].name.c_str());
FOR (j, 1, 4)
if (p[i].rd[j] < DQ) printf("%-5d", p[i].rd[j]);
else printf("     ");
puts("DQ");
CT;
}

int j = i;
int m = 0;
DB sum = 0.0;
bool flag = false;

while (j < n && p[i].total4 == p[j].total4) {
if (!p[j].amateur) {
++ m;
if (pos <= 70) {
flag = true;
sum += won[pos ++];
}
}
++ j;
}

//print [i,j)
int rank = i + 1;
while (i < j) {
printf("%s ", p[i].name.c_str());
char tmp[5];
sprintf(tmp, "%d%c", rank, m > 1 && flag && !p[i].amateur ? 'T' : ' ');
printf("%-10s", tmp);
FOR (t, 1, 4) printf("%-5d", p[i].rd[t]);

if (!p[i].amateur && flag) printf("%-10d\$%9.2lf\n", p[i].total4, sum / m);
else printf("%d\n", p[i].total4);

++ i;
}
-- i;
}
}

void Main() {
ios :: sync_with_stdio(false);

int T;
cin >> T;
while (T --) {
p.clear();
Scan();
Solve();
if (T) puts("");
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!

UVa1593

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

vector<string> words[1005];
int len[1005][200], maxlen[200];

void Main() {
int lines = 0;
string str;

while (getline(cin, str)) {
++ lines;
istringstream ss(str);
while (ss >> str) {
words[lines].pb(str);
}
}

/*
FOR (i, 1, lines) {
cout << "line #" << i << endl;
FORSTL (it, words[i]) cout << *it << endl;
cout << endl;
}
cout << endl;
*/

int maxcnt = 0;
FOR (i, 1, lines) {
int cnt = 0;
FORSTL (it, words[i]) {
++ cnt;
len[i][cnt] = (int)words[i][cnt - 1].length();
}
maxcnt = max(maxcnt, cnt);
}

/*
SHOW1(length, lines, maxcnt);
*/

FOR (j, 1, maxcnt) {
FOR (i, 1, lines) {
maxlen[j] = max(maxlen[j], len[i][j]);
}
}
FOR (i, 1, lines) {
FOR (j, 1, maxcnt) {
if (!len[i][j]) BK;
if (j > 1) cout << " ";
cout << words[i][j - 1];
if (!len[i][j + 1]) BK;
FOR (k, len[i][j] + 1, maxlen[j]) {
cout << " ";
}
}
cout << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa1594

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

int n;

void Trans(int* a) {
int b[20];
FOR (i, 1, n - 1) b[i] = abs(a[i] - a[i + 1]);
b[n] = abs(a[1] - a[n]);
FOR (i, 1, n) a[i] = b[i];
}

bool Check0(int a[]) {
FOR (i, 1, n) if (a[i]) RT false;
RT true;
}

int a[20];

void Main() {
int T;
cin >> T;
while (T --) {
cin >> n;
FOR (i, 1, n) cin >> a[i];

if (Check0(a)) { cout << "ZERO" << endl; CT;}
bool flag = false;
FOR (i, 1, 1000) {
Trans(a);
if (Check0(a)) { flag = true; BK; }
}
cout << (flag ? "ZERO" : "LOOP") << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa10935

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

int n;
queue<int> Q;

void Main() {
while (cin >> n && n) {
FOR (i, 1, n) Q.push(i);

int cnt = 0;
cout << "Discarded cards:";
while (cnt < n - 1) {
++ cnt;
int ans = Q.front();
Q.pop();

if (cnt > 1) cout << ",";
cout << " " << ans;
ans = Q.front();
Q.pop();
Q.push(ans);
}

cout << endl << "Remaining card: " << Q.front() << endl;
Q.pop();
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa10763

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

int n;
map<PII, int> m;
vector<PII> v;

void Main() {
while (scanf("%d", &n) == 1 && n) {
m.clear();

FOR (i, 1, n) {
int a, b;
Scan(a, b);
if (a > b) swap(a, b);
PII p = mp(a, b);
if (!m.count(p)) {
m[p] = 1;
v.pb(p);
}
else m[p] ^= 1;
}

bool flag = true;
FORSTL (it, v) {
PII p = *it;
if (m[p] == 1) { flag = false; BK; }
}

puts(flag ? "YES" : "NO");
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa10391

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

string str;
set<string> words;

void Main() {
while (cin >> str) {
words.insert(str);
}

FORSTL (it, words) {
str = *it;
FOR (i, 0, (int)str.size() - 1) {
string str1 = str.substr(0, i + 1), str2 = str.substr(i + 1);
//cout << str1 << ' ' << str2 << endl;
if (words.count(str1) && words.count(str2)) {
cout << str << endl;
BK;
}
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa1595

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

const int maxn = 1005;

int n, x[maxn];
map<int, set<int> > p;
PII points[maxn];

void Main() {
int T;
cin >> T;
while (T --) {
p.clear();

cin >> n;
FOR (i, 1, n) {
int xx, yy;
cin >> xx >> yy;
points[i] = mp(xx, yy);
x[i] = xx;

if (!p.count(xx)) p[xx] = set<int>();
p[xx].insert(yy);
}

sort(x + 1, x + n + 1);
int m = unique(x + 1, x + n + 1) - (x + 1);
//LIST1(x, m);
int mid;
bool flag = true;

if (m & 1) {
mid = (m + 1) >> 1;
FORSTL (it, p) {
int xx = it -> st;
FORSTL (it2, it -> nd) { //it2: set<int> :: iterator
int yy = *it2;
if (!p[x[mid] * 2 - xx].count(yy)) { flag = false; BK; }
}
}
}
else {
mid = m >> 1;
FORSTL (it, p) {
int xx = it -> st;
FORSTL (it2, it -> nd) {
int yy = *it2;
if (!p[x[mid] + x[mid + 1] - xx].count(yy)) { flag = false; BK; }
}
}
}

cout << (flag ? "YES" : "NO") << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa12100

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

int n, m;
queue<pair<int, bool> >	Q;
multiset<int> S;

void Main() {
int T;
cin >> T;
while (T --) {
while (!Q.empty()) Q.pop();
S.clear();

cin >> n >> m;
FOR (i, 0, n - 1) {
int a;
cin >> a;
Q.push(mp(a, i == m));
S.insert(a);
}

//FORSTL (it, S) cout << *it << ' ';
//cout << endl;

int ans = 0;
while (true) {
pair<int, bool> p = Q.front();
Q.pop();

bool flag = true;
FOR (i, p.st + 1, 9) {
if (S.count(i)) { flag = false; BK; }
}

if (flag) {
++ ans;
S.erase(S.find(p.st));
if (p.nd) { cout << ans << endl; BK; }
}
else Q.push(p);
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa230

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

string str, opt;
map<string, string> m;
set<pair<string, string> > S;
vector<pair<string, string> > ret;

void Main() {
while (getline(cin, str) && str != "END") {
string title, author;
istringstream ss(str);
bool flag = false;
while (ss >> str && str != "by") {
if (!flag) flag = true; else title += " ";
title += str;
}

flag = false;
while (ss >> str) {
if (!flag) flag = true; else author += " ";
author += str;
}

m[title] = author;
S.insert(mp(author, title));
//cout << title << endl;
//cout << author << endl;
}
//exit(0);
//FORSTL (it, S) cout << it -> st << ' ' << it -> nd << endl; cout << endl;
//exit(0);
while (cin >> opt && opt != "END") {
string title;

if (opt == "BORROW") {
char ch;
cin.get(ch);

getline(cin, title);
S.erase(mp(m[title], title));
//cout << title << endl;
//cout << m[title] << endl;
}
else if (opt == "RETURN") {
char ch;
cin.get(ch);

getline(cin, title);
ret.pb(mp(m[title], title));
//cout << title << endl;
//cout << m[title] << endl;
}
else { //shelve
//FORSTL (it, S) cout << "test: " << it -> nd << endl;
sort(ALL(ret));
FORSTL (it, ret) {
title = it -> nd;

//cout << title << endl;
//cout << m[title] << endl;

S.insert(mp(m[title], title));
set<pair<string, string> > :: iterator it2 = S.find(mp(m[title], title));

if (it2 != S.begin()) cout << "Put " << title << " after " << (-- it2) -> nd << endl;
else cout << "Put " << title << " first" << endl;
}
ret.clear();
cout << "END" << endl;
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa1596

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

set<char> arr;
map<char, int> sz;
set<pair<char, int> > def;
map<pair<char, int>, int> val;
int ans;
bool getans;

void Init() {
ans = 0;
getans = false;
arr.clear();
sz.clear();
def.clear();
val.clear();
}

int Getval(string str) {
bool flag = true;
FORSTL (it, str) if (!isdigit(*it)) { flag = false; BK; }

if (flag) {
int res;
istringstream ss(str);
ss >> res;
RT res;
}
int k = Getval(str.substr(2, str.size() - 3));
if (k == -1) RT -1;

if (!def.count(mp(str[0], k))) RT -1;
RT val[mp(str[0], k)];
}

void Main() {
bool flag = false;
string str;
while (getline(cin, str)) {
if (str == ".") {
if (!flag) flag = true; else BK;
if (!getans) cout << 0 << endl;
Init();
CT;
}
else flag = false;
if (getans) CT;

++ ans;

int k = str.find('=');
if (k != string :: npos) { //assignment
//cout << str.substr(0, k) << endl;
//cout << str.substr(2, k - 3) << endl;

int pos = Getval(str.substr(2, k - 3));
//cout << str << ' ' << t << endl;

int v = Getval(str.substr(k + 1));
//cout << str << ' ' << l << endl;
if (pos != -1 && v != -1 && pos < sz[str[0]]) {
def.insert(mp(str[0], pos));
val[mp(str[0], pos)] = v;
}

else { //get the ans
cout << ans << endl;
getans = true;
CT;
}
}
else { //declaration
istringstream ss(str);
char nam, t;
int s;

ss >> nam;
arr.insert(nam);
ss >> t >> s;

sz[nam] = s;

//cout << nam << ' ' << s << endl;
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa1597

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

int n, m, sta[1505];
string pas[1505]; //original
bool print[1505]; //should be print as answer
map<string, set<int> > key;
map<string, map<int, bool> > apr;

void Main() {
cin >> n;
getchar();

int lines = 0;
FOR (i, 1, n) {
string str;
sta[i] = lines + 1;

while (getline(cin, str) && str != "**********") {
pas[++ lines] = str;
string tstr = str;
FORSTL (it, tstr) *it = isalpha(*it) ? tolower(*it) : ' ';

istringstream ss(tstr);
while (ss >> str) {
if (!key[str].count(i)) key[str].insert(i);
apr[str][lines] = true;
}
}
}
sta[n + 1] = lines + 1; //[sta[i], sta[i + 1])

//FOR (i, 1, lines) cout << pas[i] << endl;
//FOR (i, 1, n) cout << sta[i] << ' ' << sta[i + 1] - 1 << endl;
//exit(0);

cin >> m;
getchar();

while (m --) {
string str, str1, str2;
getline(cin, str);
istringstream ss(str);

CLR (print, 0);

if (str.find("AND") != string :: npos) {
ss >> str1 >> str2 >> str2;

FOR (i, 1, n) {
if (!key[str1].count(i) || !key[str2].count(i)) CT;

FOR (j, sta[i], sta[i + 1] - 1) {
if (apr[str1][j] || apr[str2][j]) print[j] = true;
}
}
}
else if (str.find("OR") != string :: npos) {
ss >> str1 >> str2 >> str2;

FOR (i, 1, n) {
if (!key[str1].count(i) && !key[str2].count(i)) CT;

FOR (j, sta[i], sta[i + 1] - 1) {
if (apr[str1][j] || apr[str2][j]) print[j] = true;
}
}
}
else if (str.find("NOT") != string :: npos) {
ss >> str1 >> str1;

FOR (i, 1, n) {
if (key[str1].count(i)) CT;

FOR (j, sta[i], sta[i + 1] - 1) {
print[j] = true;
}
}
}
else {
FOR (i, 1, n) {
if (!key[str].count(i)) CT;

FOR (j, sta[i], sta[i + 1] - 1) {
if (apr[str][j]) print[j] = true;
}
}
}

bool getans = false;
FOR (i, 1, n) {
FOR (j, sta[i], sta[i + 1] - 1) {
if (print[j]) {
if (getans) cout << "----------" << endl;
getans = true;
BK;
}
}

FOR (j, sta[i], sta[i + 1] - 1) {
if (print[j]) {
cout << pas[j] << endl;
}
}
}

if (!getans) cout << "Sorry, I found nothing." << endl;
cout << "==========" << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa12504

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

string str1, str2;
map<string, string> dic1, dic2; //(name, value)
VS ans1, ans2, ans3; //+, -, *

void Init() {
dic1.clear();
dic2.clear();
ans1.clear();
ans2.clear();
ans3.clear();
}

void Work(map<string, string>& dic, string str) {
FORSTL (it, str) if (!isalpha(*it) && !isdigit(*it)) *it = ' ';
istringstream ss(str);
string name, value;
while (ss >> name >> value) dic[name] = value;
}

void Main() {
int T;
cin >> T;
while (T --) {
Init();
cin >> str1 >> str2;

Work(dic1, str1); Work(dic2, str2);

//FORSTL (it, dic1) cout << it -> st << ' '; cout << endl;
//FORSTL (it, dic2) cout << it -> st << ' '; cout << endl;

bool flag = true;

FORSTL (it, dic1) {
pair<string, string> p = *it;
string name = p.st, value = p.nd;
if (!dic2.count(name)) { //ans2
ans2.pb(name);
}
else if (dic2[name] != value) { //ans3
ans3.pb(name);
}
}
FORSTL (it, dic2) { //ans1
pair<string, string> p = *it;
string name = p.st, value = p.nd;
if (!dic1.count(name)) ans1.pb(name);
}

sort(ALL(ans1)); sort(ALL(ans2)); sort(ALL(ans3));
if (ans1.size()) { //ans1
flag = false;
cout << "+" << ans1[0];
FOR (i, 1, (int)ans1.size() - 1) cout << "," << ans1[i];
cout << endl;
}
if (ans2.size()) { //ans2
flag = false;
cout << "-" << ans2[0];
FOR (i, 1, (int)ans2.size() - 1) cout << "," << ans2[i];
cout << endl;
}
if (ans3.size()) { //ans3
flag = false;
cout << "*" << ans3[0];
FOR (i, 1, (int)ans3.size() - 1) cout << "," << ans3[i];
cout << endl;
}

if (flag) cout << "No changes" << endl;

cout << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa511

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

struct Map {
string name;
DB x1, x2, y1, y2, area, ratio;
};

vector<Map> maps;

struct Map2 {
string name;
DB area, ratio, mid, se, minx;
};

struct Cmp {
bool operator () (const Map2 A, const Map2 B) {
if (A.area != B.area) RT A.area < B.area;
if (A.mid != B.mid) RT A.mid > B.mid;
if (A.ratio != B.ratio) RT A.ratio > B.ratio;
if (A.se != B.se) RT A.se < B.se;
RT A.minx > B.minx;
}
};

map<string, int> name;

DB Sqr(DB x) { RT x * x; }

vector<Map2> ans[10010];

void Main() {
string str;
cin >> str;
while (cin >> str && str != "LOCATIONS") {
DB x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;

Map t;
t.name = str;
t.x1 = min(x1, x2);
t.x2 = max(x1, x2);
t.y1 = min(y1, y2);
t.y2 = max(y1, y2);
t.area = (t.y2 - t.y1) * (t.x2 - t.x1);
t.ratio = fabs(0.75 - (t.y2 - t.y1) / (t.x2 - t.x1));

maps.pb(t);
}

int m = 0;
while (cin >> str && str != "REQUESTS") {
DB x, y;
cin >> x >> y;

name[str] = ++ m;
HEAP<Map2, vector<Map2>, Cmp> H;

FORSTL (it, maps) {
if (x >= it -> x1 && x <= it -> x2 && y >= it -> y1 && y <= it -> y2) {
Map2 t;
t.name = it -> name;
t.area = it -> area;
t.ratio = it -> ratio;
t.mid = sqrt(Sqr((it -> x1 + it -> x2) / 2.0 - x) + Sqr((it -> y1 + it -> y2) / 2.0 - y));
t.se = sqrt(Sqr(it -> x2 - x) + Sqr(it -> y1 - y));
t.minx = it -> x1;

H.push(t);
}
}

Map2 t;
if (!H.empty()) {
ans[m].pb(t = H.top());
H.pop();
}

while (!H.empty()) {
Map2 t2 = H.top();
H.pop();
if (t.area != t2.area) ans[m].pb(t = t2);
}
}

while (cin >> str && str != "END") {
int k;
cin >> k;
cout << str << " at detail level " << k;

int pos = name[str];

if (!pos) cout << " unknown location" << endl;
else if (!ans[pos].size()) cout << " no map contains that location" << endl;
else if ((int)ans[pos].size() >= k) cout << " using " << ans[pos][k - 1].name << endl;
else cout << " no map at that detail level; using " << ans[pos][(int)ans[pos].size() - 1].name << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa822

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

struct Topic {
int tid, num, t0, t, dt, cnt;
};

vector<Topic> top;

struct Personnel {
int id, k;
VI tid;
int st, ed;

bool operator < (const Personnel B) const {
if (st == B.st) RT id < B.id;
RT st < B.st;
}
};

vector<Personnel> per;

int n, m, kase;
MII ha;

void Main() {
while (cin >> n && n) {
top.clear();
per.clear();

FOR (i, 0, n - 1) {
Topic t;
t.cnt = 0;
cin >> t.tid >> t.num >> t.t0 >> t.t >> t.dt;
ha[t.tid] = i;

top.pb(t);
}

cin >> m;
FOR (i, 0, m - 1) {
Personnel t;
int pid;

t.st = t.ed = 0;
t.id = i;
t.tid.clear();
cin >> pid >> t.k;
FOR (j, 1, t.k) {
int x;
cin >> x;
t.tid.pb(ha[x]);
}

per.pb(t);
}

int tim = 0;

while (true) {
bool getans = true;
FORSTL (it, top) if (it -> cnt < it -> num) { getans = false; BK; }
if (getans) BK;

sort(ALL(per));

FORSTL (it1, per) {
if (tim < it1 -> ed) CT;

FORSTL (it2, it1 -> tid) {
if (top[*it2].cnt >= top[*it2].num) CT;

int nexttime = top[*it2].t0 + top[*it2].dt * top[*it2].cnt;
if (tim < nexttime) CT;

++ top[*it2].cnt;
it1 -> ed = tim + top[*it2].t;
it1 -> st = tim;

BK;
}
}

++ tim;
}

int ans = 0;
FORSTL (it, per) ans = max(ans, it -> ed);

cout << "Scenario " << ++ kase << ": All requests are serviced within " << ans << " minutes." << endl;
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa1598

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

struct Command {
string opt;
int s, p;
}cmd[10010];

int s, p, id;
Buy() { s = p = 0; }
Buy(int size, int price, int i): s(size), p(price), id(i) {}

bool operator < (const Buy B) const {
if (p == B.p) RT id > B.id;
RT p < B.p;
}
};

struct Sell {
int s, p, id;
Sell() { s = 0, p = 99999; }
Sell(int size, int price, int i): s(size), p(price), id(i) {}

bool operator < (const Sell B) const {
if (p == B.p) RT id > B.id;
RT p > B.p;
}
};

int n;
HEAP<Sell> sell;
set<int> cancel;
int bnum[100010], snum[100010];

void Init() {
while (!sell.empty()) sell.pop();
cancel.clear();
CLR (bnum, 0);
CLR (snum, 0);
}

void Main() {
int kase = 0;
while (cin >> n) {
if (kase ++) cout << endl;
Init();

FOR (i, 1, n) {
string opt;
cin >> opt;
int size, price;

if (opt == "BUY") {
cin >> size >> price;
cmd[i] = (Command){"BUY", size, price};
bnum[price] += size;
}
else if (opt == "SELL") {
cin >> size >> price;
cmd[i] = (Command){"SELL", size, price};
sell.push(Sell(size, price, i));
snum[price] += size;
}
else { //CANCEL
int id;
cin >> id;
cancel.insert(id);
if (cmd[id].opt == "BUY") bnum[cmd[id].p] -= cmd[id].s;
else snum[cmd[id].p] -= cmd[id].s;
cmd[id].s = 0;
}

while (!buy.empty() && !sell.empty()) {
while (!sell.empty() && cancel.count(sell.top().id)) sell.pop();
Sell s = sell.top();
if (b.p >= s.p) {
int ts = min(cmd[b.id].s, cmd[s.id].s);
cout << "TRADE " << ts << " " << (opt == "BUY" ? s.p : b.p) << endl;
cmd[b.id].s -= ts, cmd[s.id].s -= ts;
bnum[b.p] -= ts, snum[s.p] -= ts;
if (!cmd[s.id].s) sell.pop();
}
else BK;
}
//QUOTE
while (!sell.empty() && cancel.count(sell.top().id)) sell.pop();
Sell s;
int tn1 = 0, tn2 = 0;
if (!buy.empty()) b = buy.top(), tn1 = bnum[cmd[b.id].p];
if (!sell.empty()) s = sell.top(), tn2 = snum[cmd[s.id].p];
cout << "QUOTE " << tn1 << " " << b.p << " - " << tn2 << " " << s.p << endl;
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Main();
RT 0;
}
//imagasaikou!


UVa12333

//TEMPLATE BEGIN
//#define ONLINE_JUDGE
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
//#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <functional>
#include <algorithm>
#include <complex>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); ++ (i))
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); -- (i))
#define FOREDGE(i, u, head, next) for (int (i) = (head[(u)]); (i); (i) = (next[(i)]))
#define FORSTL(it, A) for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++ it)
#define CLR(a, b) memset((a), (b), sizeof (a))
#define CPY(a, b) memcpy((a), (b), sizeof (a))
#define DEBUG cerr << "debug" << endl
#define STAR cerr << "**********" << endl

#define RT return
#define BK break
#define CT continue
#define ALL(x) (x).begin(), (x).end()
#define HEAP priority_queue
#define st first
#define nd second
#define pf push_front
#define pb push_back
#define ppf pop_front
#define ppb pop_back
#define mp make_pair
#define re real()
#define im imag()

#define LIST0(A, n) {						\
FOR (i, 0, n - 1) printf("%d ", A[i]);	\
printf("\n");							\
}											\

#define LIST1(A, n) {						\
FOR (i, 1, n) printf("%d ", A[i]);		\
printf("\n");							\
}											\

#define SHOW0(A, n, m) {					\
FOR (i, 0, n - 1) {						\
FOR (j, 0, m - 1)					\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

#define SHOW1(A, n, m) {					\
FOR (i, 1, n) {							\
FOR (j, 1, m)						\
printf("%d ", A[i][j]);			\
printf("\n");						\
}										\
}											\

typedef long long LL;
typedef double DB;
typedef long double LDB;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<DB> VB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<int, string> MIS;
typedef map<string, int> MSI;

const int INF(0x3f3f3f3f);
const LL LINF(0x3f3f3f3f3f3f3f3fLL);
const DB DINF(1e20);
const DB EPS(1e-12); //adjustable
const DB PI(acos(-1.0));

inline DB cot(DB x) { RT 1.0 / tan(x); }
inline DB sec(DB x) { RT 1.0 / cos(x); }
inline DB csc(DB x) { RT 1.0 / sin(x); }

template<class T> inline T max(T a, T b, T c) { RT max(max(a, b), c); }
template<class T> inline T max(T a, T b, T c, T d) { RT max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c) { RT min(min(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { RT min(min(a, b), min(c, d)); }

template<class T> inline void Scan(T& x) { //int, LL
char c;
for (c = getchar(); c <= ' '; c = getchar()) ;
bool ngt(c == '-');
if (ngt) c = getchar();
for (x = 0; c > ' '; c = getchar()) x = (x << 3) + (x << 1) + c - '0';
if (ngt) x = -x;
}
template<class T> inline void Scan(T& x, T& y) { Scan(x), Scan(y); }
template<class T> inline void Scan(T& x, T& y, T& z) { Scan(x), Scan(y), Scan(z); }
//TEMPLATE END

const int BASE = 10;

struct Bignum {
int dig, num[50010];

Bignum () { dig = 1, num[1] = 0;}
};

struct Node {
int id;
bool flag;
Node* ch[10];
}*root, tree[5000010];

int T;
char tstr[50010];
int front, back, cntn;

Node* NewNode() { RT &tree[++ cntn]; }

void Insert(int id, char *str, int len) {
//if (!(id % 5000)) cerr << id << endl;
//cerr << tstr + 1 << endl;

Node *x = root;
FOR (i, 1, len) {
if (!x -> ch[str[i] - '0']) x -> ch[str[i] - '0'] = NewNode();
if (!x -> id) x -> id = id;
x = x -> ch[str[i] - '0'];
}
if (!x -> id) x -> id = id;
}

int Query(string str) {
int len = (int)str.size();

Node *x = root;
FOR (i, 0, len - 1) {
if (!x -> ch[str[i] - '0']) RT -1;
x = x -> ch[str[i] - '0'];
}
RT x -> id;
}

void Print(Bignum a) {
ROF (i, back, 1) cout << a.num[i];
cout << endl;
}

Bignum a, b, c;

void Init() {
root = NewNode();
a.dig = b.dig = a.num[1] = b.num[1] = 1;

//cerr << "start" << endl;
front = back = 1;

FOR (i, 2, 99999) {
//if (!(i % 1000)) cerr << i << endl;

//c = a + b;
FOR (j, front, back) {
c.num[j] += a.num[j] + b.num[j];
if (c.num[j] >= 10) {
++ c.num[j + 1];
c.num[j] -= 10;
}
}
if (c.num[back + 1]) ++ back;
if (back - front > 50) ++ front;

//Print(c);

a = b;
b = c;

int cnt = 0, pos = back;
while (cnt <= 40 && pos > 0) tstr[++ cnt] = c.num[pos --] + '0';
Insert(i, tstr, cnt);
FOR (j, front, back + 5) c.num[j] = 0;
}

//cerr << "end" << endl;
}

void Main() {
Init();

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

cin >> T;
FOR (kase, 1, T) {
string str;
cin >> str;
cout << "Case #" << kase << ": ";
//cerr << "Case #" << kase << endl;
if (str != "1") cout << Query(str) << endl;
else cout << 0 << endl;
}
}

int main() {

Main();
RT 0;
}
//imagasaikou!


UVa212

To be continued...

#### 基于实物交互的言语康复训练系统

2015年09月26日 3.31MB 下载

11-17 135

09-27 122

#### GB4793.1-2007测量、控制和实验室用电气设备的安全要求 第1部分：通用要求

2010年01月25日 1015KB 下载

07-10 590

#### 戏剧表演与体育科学研究选题

2016年06月27日 252KB 下载

01-11 60

01-11 71

09-22 157

01-10 168