#include #include #include using namespace std; char s1[105][105],s2[105][105]; int n,m,flag[205][205],head[205],ma[205],vis[205],ans[205]; int dfs(int u) { //printf("%d\n",u); for (int i=1;i<=m;i++) { if (flag[u][i+m]) { if (vis[i+m]) continue; vis[i+m]=1; if (ma[i+m]==-1||dfs(ma[i+m])) { ma[i+m]=u; return 1; } } } return 0; } int main() { int cas; scanf("%d",&cas); while (cas--) { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%s%s",s1[i]+1,s2[i]+1); memset(flag,0,sizeof(flag)); for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) { for (int k=1;k<=n;k++) { if (s1[k][i]!=s2[k][j]) { goto fail; } } flag[i][j+m]=1; //printf("%d %d\n",i,j); fail:; } int Ans=0; memset(ma,-1,sizeof(ma)); for (int i=m;i;i--) { for (int j=1;j<=m+m;j++) vis[j]=0; Ans+=dfs(i); } if (Ans==m) { for (int i=1;i<=m;i++) ans[ma[i+m]]=i; for (int i=1;i