#include <set>#include <map>#include <string>#include <cctype>#include <cstdio>using namespace std;int main(void){ int re; scanf("%d", &re); for (int ri = 1; ri <= re; ++ri) { set <string> st; map <string, string> mp; int n; char ch, str[10], buf[10]; scanf("%d ", &n); while(n--) { gets(str); string tmp(str); int l = tmp.length(); int ll = 1 << l; for (int mask = 1; mask < ll; mask++) { int len = 0; for (int bit = 0; bit < l; bit++) if(mask & (1 << bit)) buf[len++] = str[bit]; buf[len] = ' '; string cur(buf); if(st.find(cur) == st.end()) { map<string, string>::iterator itr = mp.find(cur); if(itr == mp.end()) mp.insert(make_pair(cur, tmp)); else if(itr -> second != tmp) { st.insert(cur); mp.erase(itr); } } } } string ans = ""; ch = getchar(); while(true) { if(isupper(ch)) { int len = 0; do { buf[len++] = ch; ch = getchar(); }while(isupper(ch)); buf[len] = ' '; map<string, string>::iterator itr = mp.find((string)buf); if(itr == mp.end()) { puts("AMBIGUITY"); while(ch != '#') ch = getchar(); break; } else ans += itr -> second; } else if(ch == '#') { puts(ans.c_str()); break; } else { ans += ch; ch = getchar(); } } if(ri < re) putchar('n'); } return 0;}