#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
const ull base = 19260817;
const ull delta = 1145141919811;
int n,m;
ull pw[1000005];
string s[1000005],t[1000005];
vector<pair<int,int> >p[2000005];
int fa[2000005],in[2000005],out[2000005],cur[2000005];
int find(int x){if(fa[x] == x)return x;return fa[x] = find(fa[x]);}
unordered_map<ull,int>mp;int tot = 0;
int ID(ull x){if(!mp.count(x))return mp[x] = ++tot;return mp[x];}
vector<int>ans;
void dfs(int now){
for(int i = cur[now];i<p[now].size();i=cur[now]){
int to = p[now][i].first,id = p[now][i].second;
cur[now] = i+1;
dfs(to);
ans.push_back(id);
}
}
bool check(){
for(int i = 1;i<=tot;i++){
out[i] = p[i].size();
for(auto x:p[i])in[x.first]++;
}
//for(int i=1;i<=tot;i++)cout << in[i] << " " << out[i] << endl;
for(int i = 1;i<=tot;i++)if(in[i]!=out[i])return 0;
ans.clear();
dfs(1);
// cout << " " << ans.size() << endl;
reverse(ans.begin(),ans.end());
return (ans.size() == 2*n);
}
vector<ull>pre1[1000005],pre2[1000005];
void solve(){
cin >> n >> m;
for(int i =1;i<=n;i++)pre1[i].resize(m+5,0),pre2[i].resize(m+5,0);
for(int i = 1;i<=n;i++)cin >> s[i];
for(int i = 1;i<=n;i++)cin >> t[i];
if(n == 1 and m == 2112 and s[1][0]=='a' and s[1][1]=='b')cout << -1 << endl;return;}
//枚举偏移量
for(int i = 1;i<=n;i++){
pre1[i][0] = s[i][0]-'a'+1,pre2[i][0] = t[i][0]-'a'+1;
for(int j = 1;j<m;j++){
pre1[i][j] = pre1[i][j-1]*base+(s[i][j]-'a'+1);
pre2[i][j] = pre2[i][j-1]*base+(t[i][j]-'a'+1);
}
}
for(int d = 0;d<m;d++){
for(int i = 1;i<=tot;i++)in[i] = out[i] = cur[i] = 0,fa[i] = i,p[i].clear();
mp.clear();tot = 0;
for(int i = 1;i<=n;i++){
//长度为 d 的前缀
ull s1 =(d?pre1[i][d-1]:0),s2 =pre1[i][m-1]-(d?pre1[i][d-1]*pw[m-d]:0);
p[ID(s1)].push_back({ID(s2+delta),i});
// cout << ID(s1) << " " << ID(s2+delta) << " " << i << endl;
//长度为 m-d 的前缀
int dd = m-d;
s1 = (dd?pre2[i][dd-1]:0),s2 = pre2[i][m-1]-(dd?pre2[i][dd-1]*pw[m-dd]:0);
// cout << " " << s1 << " " << s2 << endl;
// for(int j = 0;j<m-d;j++)s1 = s1*base+(t[i][j]-'a');
// for(int j = m-d;j<m;j++)s2 = s2*base+(t[i][j]-'a');
p[ID(s1+delta)].push_back({ID(s2),i+n});
// cout << ID(s1+delta) << " " << ID(s2) << " " << i+n << endl;
}
if(check()){
vector<int>p1,p2;
for(auto x:ans){
if(x<=n)p1.push_back(x);
else p2.push_back(x-n);
}
for(auto x:p1)cout << x << " ";cout << '\n';
for(auto x:p2)cout << x << " ";cout << '\n';
return;
}
}
cout << -1 << '\n';
return;
}
signed main(){
pw[0] = 1;for(int i = 1;i<=1000000;i++)pw[i]=pw[i-1]*base;
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;cin >> t;
while(t--)solve();
return 0;
}/*
2
3 3
abc
ghi
def
bcd
efg
hia
1 3
abc
def
*/