//NWERC-2007-H

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

#define VI vector<int>
#define int64 long long
#define VVI vector <vector<int> >

#define SZ(a) a.size()
#define MP make_pair
#define PB push_back
#define ALL(a) a.begin(), a.end()
#define CLA(a) memset(a, 0, sizeof(a))
#define FOR(i,n) for (int i = 0; i < int(n); i++)
#define FORR(i,n) for (int i = n - 1; i >=0; i--)
#define REP(n) for (int _foo = n - 1; _foo >= 0; _foo--)

#define INF 1000000000

int ri() {int t; scanf("%d", &t); return t;}
void rs(char *s) {scanf("%s", s);}
void pi(int n){printf("%d\n", n);}

int n, len;

char s[1024];

char d[10*1024][128];

int ln[10240];

int dp[1024];
int prev[1024], r[1024], res;

int q[256];

inline int cmp(int k, int pos)
{
	int len = ln[k];
	if (s[pos] != d[k][0]) return 0;
	if (s[pos+len-1] != d[k][len-1]) return 0;

	CLA(q);
	
	for (int i = 1; i < len - 1; i++)
		q[s[pos+i]]++;

	for (int i = 1; i < len - 1; i++)
	{
		char c = d[k][i];
		if (!q[c]) return 0;
		q[c]--;
	}

	return 1;
}

int calc(int k)
{
	if (k == len) return 1;
	if (k > len) return -INF;

	int &res = dp[k];

	if (!res)
	{
		res = 0;
		FOR(i,n)
		{
			if (!cmp(i, k)) continue;
			int t = calc(k + ln[i]);
			if (t > 0)
			{
				prev[k] = i;
				res += min(t, 2);
			}
		}
		if (!res) res = -INF;
	}

	return res;
}

void solve()
{
	CLA(dp);
	len = strlen(s);
	calc(0);
	if (dp[0] > 1) res = -1;
	else if (dp[0] < 0) res = 0;
	else
	{
		int pos = 0; res = 0;
		while (pos < len)
		{
			r[res++] = prev[pos];
			pos += ln[prev[pos]];
		}
	}
}

int main()
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif

	REP(ri())
	{
		rs(s);
		n = ri();
		FOR(i,n)
		{
			rs(d[i]);
			ln[i] = strlen(d[i]);
		}
		solve();
		if (res > 0)
		{
			FOR(i,res)
				printf("%s%c", d[r[i]], i == res - 1 ? '\n' : ' ');
		}
		else if (res == -1)
		{
			puts("ambiguous");
		}
		else
			puts("impossible");
		//pi(res);
	}

}