#include #include #include #include #include using namespace std; int a[60][60]; int ans[60]; int n, m; char s1[60],s2[60]; int vis[60]; vector vec[60]; int dfs(int x,int y) { vis[y] = 1; if (x == m)return 1; for (int i = 0; i < vec[x+1].size(); i++) { if (vis[vec[x+1][i]])continue; ans[x+1] = vec[x+1][i]; if (dfs(x + 1,ans[x+1]))return 1; } vis[y] = 0; return 0; } int main() { int t; cin >> t; while (t--) { cin >> n >> m; for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { a[i][j] = 1; } vis[i] = 0; vec[i].clear(); } for (int k = 1; k <= n; k++) { cin >> s1 + 1 >> s2 + 1; for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { if (s1[i] != s2[j])a[i][j] = 0; } } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { if (a[i][j])vec[i].push_back(j); } } int flg1 = 1; for (int i = 1; i <= m; i++) { int flg = 1; for (int j = 1; j <= m; j++) { if (a[i][j] == 1) { flg = 0; break; } } if (flg)flg1 = 0; } for (int i = 1; i <= m; i++) { int flg = 1; for (int j = 1; j <= m; j++) { if (a[j][i] == 1) { flg = 0; break; } } if (flg)flg1 = 0; } if (flg1 == 0) { cout << -1 << endl; continue; } if(flg1)for (int i = 1; i <= m; i++) { if (a[1][i] == 1) { ans[1] = i; if (dfs(1,i)) { flg1 = 0; break; } } } if (!flg1) { for (int i = 1; i < m; i++)cout << ans[i] << " "; cout << ans[m] << endl; } else cout << -1 << endl; } }