#include <bits/stdc++.h>
#include <iostream>
#define ll long long
using namespace std;
int main()
{
ll t;
cin >> t;
while (t--)
{
ll n, m;
cin >> n >> m;
int row[n][m] = {0};
int col[m][n] = {0};
int mat[n][m] = {0};
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> row[i][j];
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> col[i][j];
}
}
for (int i = 0; i < m; i++)
{
// set<int> s;
// map<int, int> ma;
// for (int j = 0; j < n; j++)
// {
// ma[row[j][i]]++;
// }
int first=row[0][i];
int ind = -1;
for (int k = 0; k < m; k++)
{
ind =k;
int flag=0;
//map<int, int> ma1;
for (int i = 0; i < n; i++)
{
//ma1[col[k][i]]++;
if(col[k][i]==first)
{
//ind=k;
flag=1;
break;
}
}
if(flag==1)
{
//flag=0;
break;
}
}
for (int y = 0; y < n; y++)
{
mat[y][i] = col[ind][y];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
1408B - Arrays Sum | 1430A - Number of Apartments |
1475A - Odd Divisor | 1454B - Unique Bid Auction |
978C - Letters | 501B - Misha and Changing Handles |
1496A - Split it | 1666L - Labyrinth |
1294B - Collecting Packages | 1642B - Power Walking |
1424M - Ancient Language | 600C - Make Palindrome |
1669D - Colorful Stamp | 1669B - Triple |
1669A - Division | 1669H - Maximal AND |
1669E - 2-Letter Strings | 483A - Counterexample |
3C - Tic-tac-toe | 1669F - Eating Candies |
1323B - Count Subrectangles | 991C - Candies |
1463A - Dungeon | 1671D - Insert a Progression |
1671A - String Building | 1671B - Consecutive Points Segment |
1671C - Dolce Vita | 1669G - Fall Down |
4D - Mysterious Present | 1316B - String Modification |