照着大白打一发...
/* Pigonometry */
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int n, pref[maxn][maxn], order[maxn][maxn], next[maxn], husband[maxn], wife[maxn];
queue<int> q;
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void engage(int man, int woman) {
int m = husband[woman];
if(m) {
wife[m] = 0;
q.push(m);
}
husband[woman] = man;
wife[man] = woman;
}
int main() {
int T = iread();
while(T--) {
n = iread();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) pref[i][j] = iread();
next[i] = 1;
wife[i] = 0;
q.push(i);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) order[i][iread()] = j;
husband[i] = 0;
}
while(!q.empty()) {
int man = q.front(); q.pop();
int woman = pref[man][next[man]++];
if(!husband[woman]) engage(man, woman);
else if(order[woman][man] < order[woman][husband[woman]]) engage(man, woman);
else q.push(man);
}
for(int i = 1; i <= n; i++) printf("%d\n", wife[i]);
if(T) printf("\n");
}
return 0;
}