//12252024832524 #include #include #include #define Min(x,y) (xy?x:y) using namespace std; typedef long long LL; const int MAXN = 55; int n,m; int vis[MAXN],ans[MAXN]; bool vis1[MAXN][MAXN],vis2[MAXN][MAXN]; bool used[MAXN]; char a[MAXN],b[MAXN]; int Read() { int x1 = 0,f1 = 1;char c1 = getchar(); while(c1 > '9' || c1 < '0'){if(c1 == '-')f1 = -1;c1 = getchar();} while(c1 >= '0' && c1 <= '9'){x1 = (x1*10) + (c1^48);c1 = getchar();} return x1 * f1; } void Put(int x) { if(x > 9) Put(x/10); putchar(x%10^48); } bool dfs(int x) { if(!x) return 1; for(int i = m;i >= 1;-- i) { if(vis1[x][i] && !used[i]) { used[i] = 1; if(dfs(vis[i])) { vis[i] = x; return 1; } } } return 0; } int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); for(int T = Read(); T ;-- T) { n = Read(); m = Read(); memset(vis,0,sizeof(vis)); memset(vis1,0,sizeof(vis1)); scanf("%s %s",a+1,b+1); for(int i = 1;i <= m;++ i) for(int j = 1;j <= m;++ j) if(a[i] == b[j]) vis1[i][j] = 1; for(int i = 2;i <= n;++ i) { scanf("%s %s",a+1,b+1); for(int j = 1;j <= m;++ j) for(int k = 1;k <= m;++ k) if(a[j] == b[k]) vis2[j][k] = 1; for(int j = 1;j <= m;++ j) for(int k = 1;k <= m;++ k) vis1[j][k] &= vis2[j][k]; memset(vis2,0,sizeof(vis2)); } for(int i = 1;i <= m;++ i) { memset(used,0,sizeof(used)); if(!dfs(i)) { puts("-1"); break; } if(i == m) { for(int j = 1;j <= m;++ j) ans[vis[j]] = j; for(int j = 1;j < m;++ j) printf("%d ",ans[j]); printf("%d\n",ans[m]); } } } return 0; }