#include #include #include #include using namespace std; const int maxn = 110; struct BPM { vector g[maxn]; int n, m, l[maxn]; bool vis[maxn]; void init() { for (int i = 0; i < maxn; ++i) g[i].clear(); } bool match(int x) { for (int y : g[x]) if (!vis[y]) { vis[y] = true; if (l[y] == -1 || match(l[y])) { l[y] = x; return true; } } return false; } int solve() { memset(l, -1, sizeof l); int ans = 0; for (int i = n - 1; i >= 0; --i) { memset(vis, 0, sizeof vis); if (match(i)) ans++; } return ans; } } s; void solve() { int n, m; cin >> n >> m; static bool g[111][111]; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { g[i][j] = true; } } bool gg = false; for (int i = 0; i < n; ++i) { string a, b; cin >> a >> b; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (a[i] != b[j]) { g[i][j] = false; } } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (a != b) gg = true; } if (gg) { cout << -1 << endl; return; } s.init(); s.n = m * 2; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (g[i][j]) s.g[i].push_back(j + m); } } if (s.solve() != m) { cout << -1 << endl; } else { vector ans(m); for (int i = 0; i < m; ++i) { ans[s.l[i + m]] = i + 1; } for (int i = 0; i < m; ++i) { cout << ans[i] << (i == m - 1 ? '\n' : ' '); } } } int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T-- > 0) { solve(); } return 0; }