#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PB push_back #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define REP(i, n) for(int i=0; i=0; --i) #define FOR(i, b, e) for(typeof(e) i=b; i!=e; ++i) using namespace std; using namespace tr1; typedef long long LL; typedef vector VI; typedef vector VVI; typedef vector VS; typedef istringstream ISS; string canon(string s) { if(SZ(s)<=2) return s; sort(s.begin()+1,s.end()-1); return s; } string ec; string dc; vector pos; VS sstack; vector cache; LL DFS(int p) { //cout << ec.substr(p) << endl; if(p==SZ(ec)) { if(SZ(dc)==0) { dc=sstack[0]; FOR(i, 1, SZ(sstack)) dc+=' '+sstack[i]; } return 1; } if(cache[p]!=-1) return cache[p]; LL res=0; REP(i, SZ(pos[p])) { sstack.PB(pos[p][i]); res+=DFS(p+SZ(pos[p][i])); sstack.pop_back(); } cache[p]=res; return res; } void prepare() { int N; cin >> ec >> N; pos.clear(); pos.resize(SZ(ec)); REP(i, N) { string d; cin >> d; string c=canon(d); int s=SZ(d); REP(j, SZ(ec)-SZ(d)+1) { if(d[0]==ec[j] && d[s-1]==ec[j+s-1] && c==canon(ec.substr(j, SZ(c)))) { pos[j].PB(d); } } } } void go() { prepare(); dc.clear(); sstack.clear(); cache.clear(); cache.resize(SZ(ec),-1); int n=DFS(0); if(n==0) cout << "impossible\n"; else if(n==1) cout << dc << endl; else cout << "ambiguous\n"; } int main() { int nruns; scanf("%d", &nruns); while(nruns--) go(); return 0; }