/*
URL_HERE
*/
/*

*/
#ifdef _WIN32
#pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <queue>
#include <iostream>
using namespace std;

FILE* _fin = stdin;
FILE* _fout = stdout;
#define PI 3.141592653589793238462643383279502884197169399375105820974
#define ten5p1 100001
#define ten6p1 1000001
#define ten8p1 100000001
#define ten9p1 1000000001
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define rep(var,n) for(int var=0;var<n;++var)
#define repi(n) rep(i,n)
#define repj(n) rep(j,n)
#define repi1(n) for(int i=1;i<n;++i)
#define repj1(n) for(int j=1;j<n;++j)
int _min(int a, int b) { return a <= b ? a : b; }
ll _min(ll a, ll b) { return a <= b ? a : b; }
int _max(int a, int b) { return a >= b ? a : b; }
ll _max(ll a, ll b) { return a >= b ? a : b; }
void zero(int* data, int n) { memset(data, 0, sizeof(int) * n); }
void zero(ll* data, int n) { memset(data, 0, sizeof(ll) * n); }
void zero(unsigned long long* data, int n) { memset(data, 0, sizeof(unsigned long long) * n); }
void zero(char* data, int n) { memset(data, 0, sizeof(char) * n); }
char readc() { char var; fscanf(_fin, "%c", &var); return var; }
int readi() { int var; fscanf(_fin, "%d", &var); return var; }
double readlf() { double var; fscanf(_fin, "%lf", &var); return var; }
ll readll() { ll var; fscanf(_fin, "%lld", &var); return var; }
void repread(int* data, int n) { repi(n) data[i] = readi(); }
void repread(ll* data, int n) { repi(n) data[i] = readll(); }
int reads(char* str, int maxsize)
{
	for (;;) { if (fgets(str, maxsize, _fin) == NULL) break; if (str[0] != '\n' && str[0] != '\r') break; }
	int slen = strlen(str); if (slen == 0) return 0;
	if (str[slen - 1] == '\n' || str[slen - 1] == '\r') str[--slen] = 0;
	return slen;
}

#define writec(var) fprintf(_fout,"%c",var)
#define writecsp(var) fprintf(_fout,"%c ",var)
#define writecln(var) fprintf(_fout,"%c\n",var)
#define writei(var) fprintf(_fout,"%d",var)
#define writeisp(var) fprintf(_fout,"%d ",var)
#define writellsp(var) fprintf(_fout,"%lld ",var)
#define writeiln(var) fprintf(_fout,"%d\n",var)
#define writellln(var) fprintf(_fout,"%lld\n",var)
#define writeulln(var) fprintf(_fout,"%llu\n",var)
#define writefln(var) fprintf(_fout,"%f\n",var)
#define writes(str) fprintf(_fout,"%s",str)
#define writesp() fprintf(_fout," ")
#define writeln() fprintf(_fout,"\n")
#define RUN_LOCAL(testfilename) {_fin = fopen(testfilename, "r"); if(_fin==NULL) _fin=stdin;}
#define swap(type,a, b) {type t=a;a=b;b=t;}
#define sort(data,n) std::sort(data,data+n)
//#define mod(a,b) b==0 ? 0 : a==0 ? 0 :(a>0 ? a%b : b + a % b) //what the hell is this...
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return (a / g) * b; }
void reverse(char* data, int n) { int k = n >> 1; repi(k) { char tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
void reverse(int* data, int n) { int k = n >> 1; repi(k) { int tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
void reverse(ll* data, int n) { int k = n >> 1; repi(k) { ll tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }

#define _Vec(type, name) \
struct name { type* data; int size; int n; };\
void init(name* t, int size) { t->data = (type*)malloc(sizeof(type) * size); t->size = size; t->n = 0; }\
void resize(name* t) { int ns = t->size * 1.2f; t->data = (type*)realloc(t->data, sizeof(type) * ns); t->size = ns; }\
void add(name* t, type val) { if (t->n >= t->size) resize(t); int k = t->n; t->data[k] = val; t->n = k + 1; }\
void free(name* t) { free(t->data);}
_Vec(int, Veci)
_Vec(long long, Vecll)
_Vec(char*, Vecs)

#define _ispal(type, name) int name(type* a, type* b, int n){repi(n){if (a[i] != b[n - i - 1]){return 0;}}return 1;}
_ispal(int, ispali)
_ispal(char, ispalc)

#define _Pair(type, name) struct name{type x, y;}; \
int cmp_##name(const void* _a, const void* _b)\
{\
	name* a = (name*)_a;name* b = (name*)_b;\
	if (a->x < b->x) return -1;\
	else\
	{\
		if (a->x == b->x)\
		{\
			if (a->y == b->y) return 0;\
			if (a->y < b->y) return -1;\
			else return 1;\
		}\
		else\
		{\
			return 1;\
		}\
	}\
}\
void sort_##name(name* d, int n){qsort(d, n, sizeof(name), cmp_##name);}
	_Pair(int, Pairi)
	_Pair(double,Pairlf)

	int cmp_Str(const void* _a, const void* _b) { char* a = *((char**)_a); char* b = *((char**)_b); return strcmp(a, b); }
void sort_Str(char** str, int n) { qsort(str, n, sizeof(char*), cmp_Str); }

ll expmod(ll x, ll n, ll m) {
	ll ans = 1;
	for (; n;) {
		if (n & 1) ans = (ans * x) % m;
		x = (x * x) % m;
		n >>= 1;
	}
	return ans;
}

ll combmod(ll n, ll k, ll m) {
	ll ret = 1;
	ll div = 1;
	for (ll i = 0; i < k; ++i) {
		ret = (ret * (n - i) % m) % m;
		div = (div * (i + 1)) % m;
	}
	div = expmod(div, m - 2, m) % m;
	return (ret * div) % m;
}

int next_perm(int* data, int n)
{
	//find first i,i+1 where d[i]<d[i+1]
	int rootidx = -1;
	for (int i = n - 2; i >= 0; --i)
	{
		if (data[i] < data[i + 1])
		{
			rootidx = i;
			break;
		}
	}

	if (rootidx == -1) return 0;

	int ceilingidx = rootidx + 1;
	//find smallest d[x] after d[rooti] where d[x]>d[rooti]
	for (int i = rootidx + 2; i < n; ++i)
	{
		if (data[i] > data[rootidx] && data[i] < data[ceilingidx])
		{
			ceilingidx = i;
		}
	}

	swap(int, data[rootidx], data[ceilingidx]);
	sort(&data[rootidx + 1], n - rootidx - 1);
	return 1;
}
int count_digits(ll a)
{
	int k = 0;
	for (;;)
	{
		if (a == 0)break;
		k++;
		a /= 10;
	}
	return k;
}

/*
use only when val EXISTS in data
*/
int bs_exists(int* data, int n, int val)
{
	int left = 0;
	int right = n - 1;
	for (; left < right;)
	{
		int mid = (left + right + 1) >> 1;
		if (data[mid] <= val) left = mid;
		else right = mid - 1;
	}
	return right;
}

ll simple_factorial(int x)
{
	ll f = 1.0;
	repi(x)
	{
		f *= (i + 1);
	}
	return f;
}

unsigned long long djb2(char* data, int n)
{
	unsigned long long h = 5381;
	for (int i = 0; i < n; ++i) h = (h << 5) + h + data[i];
	return h;
}

struct Hashcount
{
	int* count;
	unsigned long long* keys;
	int size_bits;
};
void init(Hashcount* h, int size_bits)
{
	h->size_bits = size_bits;
	int size = 1 << h->size_bits;
	h->count = (int*)malloc(sizeof(int) * size);
	zero(h->count, size);
	h->keys = (unsigned long long*)malloc(sizeof(unsigned long long) * size);
	zero(h->keys, size);
}

int set(Hashcount* h, unsigned long long key)
{
	//knuth's golden ratio multiplicative hashing for 64-bit keys
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key) //if key exists, overwrite data
		{
			h->count[index]++;
			return index;
		}

		if (h->keys[index] == 0) //empty spot
		{
			h->count[index] = 1;
			h->keys[index] = key;
			return index;
		}

		index++;
		index &= size - 1;
	}

	return -1;//return invalid index if data was not placed. this could be used as an indicator to resize.
}

int get(Hashcount* h, unsigned long long key)
{
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key)//found original key
		{
			return h->count[index];
		}

		if (h->keys[index] == 0) return 0;//if you see a zero,key was never there

		index++;
		index &= size - 1;
	}
	return 0;
}
//not sure if this works
int sub(Hashcount* h, unsigned long long key)
{
	int index = (key * 2654435761) >> (64 - h->size_bits);
	int size = 1 << h->size_bits;
	for (int i = 0; i < size; i++)
	{
		if (h->keys[index] == key)//found original key
		{
			--h->count[index];
			if (h->count[index] < 0) h->count[index] = 0;
			return h->count[index];
		}

		if (h->keys[index] == 0) return 0;//if you see a zero,key was never there

		index++;
		index &= size - 1;
	}
	return 0;
}

const ll MOD  = 1e9 + 7;
/*
//proper binary search???
//tested on 1 problem it works!!!!!!!
int left = 0;
int right = n-1;
for (;;)
{
	if (left >= right)break;
	int mid = (left + right) >> 1;
	if (x <= a[mid]) right = mid;
	else left = mid+1;
}

printf("%d %d\n",right, a[right]);
*/

//------------------------------------------

//------------

int main()
{

	RUN_LOCAL("input.txt");
	int t = readi();
	rep(_t, t)
	{
		int n = readi();
		int x = readi();
		printf("%d\n", x * 2);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        map<int,int>p;
        int n,x;
        scanf("%d%d",&n,&x);
        int ans=n;
        for(int i=1;i<=n;i++)
        {
            p[i]=i;
        }
        int tmp;
        for(int i=1;i<=n;i++)
        {
         tmp=i+i-1;
         p[tmp]=0;
         ans--;
         if(ans<i)
         break;
        }
        ans=0;
        for(int i=1;i<=n;i++)
        {
          if(p[i]!=0)
          {
              ans++;
          }
          if(ans==x)
          {
              printf("%d\n",p[i]);
              break;
          }
        }

    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n,x;
        scanf("%lld%lld",&n,&x);
       printf("%lld\n",2*x);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        map<long long,long long>p;
        long long n,x;
        scanf("%lld%lld",&n,&x);
        long long ans=n;
        for(long long i=1;i<=n;i++)
        {
            p[i]=i;
        }
        long long tmp;
        for(long long i=1;i<=n;i++)
        {
         tmp=i+i-1;
         p[tmp]=0;
         ans--;
         if(ans<i)
         break;
        }
        ans=0;
        for(long long i=1;i<=n;i++)
        {
          if(p[i]!=0)
          {
              ans++;
          }
          if(ans==x)
          {
              printf("%lld\n",p[i]);
              break;
          }
        }

    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
map<int,int>p;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,x;
        scanf("%d%d",&n,&x);
        int ans=0;
        int tmp=1;
        for(int i=1;i<=n;i++)
        {
         if(i==tmp+tmp-1)
         {
             tmp++;
         }
         else
         {
           ans++;
           if(ans==x)
           {
               printf("%d\n",i);
               break;
           }
         }
        }

    }
    return 0;
}


#include<cstdio>
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,x,sum=0,ans=0;
        scanf("%lld%lld",&n,&x);
        int ssum=n;
        for(int i=2;i<=n;i+=2)
        {
            ssum--;
            sum++;
            if(ssum==x)
            {
                ans=n;
                break ;
            }
            if(sum==x)
            {
                ans=i;
                break ;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,x,sum=0,ans=0;
        scanf("%d%d",&n,&x);
        int ssum=n;
        for(int i=2;i<=n;i+=2)
        {
            ssum--;
            sum++;
            if(ssum==x)
            {
                ans=n;
                break ;
            }
            if(sum==x)
            {
                ans=i;
                break ;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<cstdio>
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,x,sum=0,ans=0;
        scanf("%d%d",&n,&x);
        if(x*2<=n)
            ans=x*2;
        else
            ans=n;
        printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>

int main()
{
	int C;
	scanf("%d", &C);

	while (C--)
	{
		int x, n;
		scanf("%d%d", &n, &x);

		printf("%d\n", 2 * x);
	}

	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int q;
	cin >> q;
	while (q--) {
		int n, k;
		cin >> n >> k;
		cout << k * 2;
	}
}


#include <iostream>
using namespace std;

int main()
{
	int q;
	cin >> q;
	while (q--) {
		int n, k;
		cin >> n >> k;
		cout << k * 2 << "\n";
	}
}


#include <iostream>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		long n;
		long x;
		cin >> n >> x;
		cout << 2 * x<< endl;
	}
	return 0;
}


#include<iostream>
#include<queue>
#include<vector>
#include<string.h>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<memory.h>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,x;
        scanf("%d%d",&n,&x);
        printf("%d\n",2*x);
    }
}


#include <iostream>

using namespace std;

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int n, x;
		cin >> n >> x;
		cout << x * 2 << endl;
	}
	//system("pause");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;

int main()
{
	int T;
	long long a, b;
	cin >> T;
	for (int i = 0; i < T; i++)
	{
		cin >> a >> b;
		int c = 0;
		for (int i = 0; i < b; i++)
		{
			c += 2;
		}
		cout << c << endl;
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;

int main()
{
	int T;
	long long a, b;
	cin >> T;
	for (int i = 0; i < T; i++)
	{
		cin >> a >> b;
		cout << b * 2 << endl;
	}
}


#include<iostream>
using namespace std;
int main(){
    int a,b,c;
    cin>>a;
    for(int i=0;i<a;i++){
    cin>>b>>c;
    cout<<c*2<<endl;
    }
return 0;
}


#include <iostream>

int main()
{
    using namespace std;
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
        {
            int a,b;
            cin>>a>>b;
            cout<<b*2<<endl;
        }
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int T;
	cin >> T;
	int* rez = new int[T];
	for (int i = 0; i < T; i++)
	{
		int n;
		cin >> n;
		int x;
		cin >> x;
		rez[i] = 2 * x;
	}
	for (int i = 0; i < T; i++)
	{
		cout << rez[i] << endl;
	}
	delete[] rez;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <string>

//#define vec vector<int>

using namespace std;

int main()
{
    int n, x, t;
    cin >> t;
    while (t--) {
        cin >> n >> x;
        cout << x * 2 << endl;
    }
}


#include <iostream>
using namespace std;
int main() {
    int t;
    cin >> t;
    while(t--) {
        int n, x;
        cin >> n >> x;
        cout << 2*x << endl;
    }

return 0;
}


#include <iostream>

using namespace std;
int main() {
	int t;
	cin >> t;

	for (int i = 0; i < t; i++) {
		int n, x;
		cin >> n >> x;
		cout << x * 2 << "\n";
	}

		return 0;
	}


#include<stdio.h>
int main()
{
    int t,i;
    scanf("%d",&t);
    long long int n,x;
    for(i=1;i<=t;i++)
    {
        scanf("%lld %lld",&n,&x);
        long long int ans=x*2;
        printf("%lld\n",ans);
    }
}


#include <iostream>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<long long, long long>
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define int long long
#define _ << " " <<
#define $ << " "
#define all(a) a.begin(), a.end()
#define srt(a) std::sort(a.begin(), a.end())
template<typename T>
vector<T> _set_to_vec(std::set<T> z)
{
	vector<T> w;
	auto it = z.begin();
	while (it != z.end())
		w.push_back(*it++);
	return w;
}
template<typename T>
void pop_front(std::vector<T>& vec)
{
	vec.erase(vec.begin());
}

vector<long long> z_function(string s) {
	long long n = (long long)s.length();
	vector<long long> z(n);
	for (long long i = 1, l = 0, r = 0; i < n; ++i) {
		if (i <= r)
			z[i] = min(r - i + 1, z[i - l]);
		while (i + z[i] < n && s[z[i]] == s[i + z[i]])
			++z[i];
		if (i + z[i] - 1 > r)
			l = i, r = i + z[i] - 1;
	}
	return z;
}
struct point {
	double x, y;
	point(double x, double y) :x(x), y(y) {};
	point() {};
};

point operator+(point a, point b) { return point(a.x + b.x, a.y + b.y); };
point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y); };
double operator*(point a, point b) { return (a.x * b.x + a.y * b.y); };

double operator^(point a, point b) { return (a.x * b.y - a.y * b.x); };
double operator%(point a, point b) { return atan2(a^b, a*b); }
double eps = 1e-13;
template<typename T>
bool equals(T a, T b)
{
	return abs(a - b) <= eps;
}
template<typename T>
int sign(T a)
{
	return a < 0 ? -1 : (a > 0 ? 1 : 0);
}
#define pt point
inline double area(pt a, pt b, pt c) {
	return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
inline char intersect_1(double a, double b, double c, double d) {
	if (a > b)  swap(a, b);
	if (c > d)  swap(c, d);
	return max(a, c) <= min(b, d);
}

char intersect(pt a, pt b, pt c, pt d) {
	return intersect_1(a.x, b.x, c.x, d.x)
		&& intersect_1(a.y, b.y, c.y, d.y)
		&& area(a, b, c) * area(a, b, d) <= 0
		&& area(c, d, a) * area(c, d, b) <= 0;
}

double pi = acos(-1.0);
double dist(point p)
{
	return sqrt(p.x * p.x + p.y*p.y);
}

bool cmpr(pt a, pt b)
{
	if (equals((a^b), (double)0))
		return dist(a) < dist(b);
	else return (a ^ b) < 0;
}
bool cmp(pt a, pt b) {
	return a.x < b.x || a.x == b.x && a.y < b.y;
}

bool cw(pt a, pt b, pt c) {
	return a.x*(b.y - c.y) + b.x*(c.y - a.y) + c.x*(a.y - b.y) < 0;
}

bool ccw(pt a, pt b, pt c) {
	return a.x*(b.y - c.y) + b.x*(c.y - a.y) + c.x*(a.y - b.y) > 0;
}

void convex_hull(vector<pt> & a) {
	if (a.size() == 1)  return;
	sort(a.begin(), a.end(), &cmp);
	pt p1 = a[0], p2 = a.back();
	vector<pt> up, down;
	up.push_back(p1);
	down.push_back(p1);
	for (size_t i = 1; i < a.size(); ++i) {
		if (i == a.size() - 1 || cw(p1, a[i], p2)) {
			while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], a[i]))
				up.pop_back();
			up.push_back(a[i]);
		}
		if (i == a.size() - 1 || ccw(p1, a[i], p2)) {
			while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], a[i]))
				down.pop_back();
			down.push_back(a[i]);
		}
	}
	a.clear();
	for (size_t i = 0; i < up.size(); ++i)
		a.push_back(up[i]);
	for (size_t i = down.size() - 2; i > 0; --i)
		a.push_back(down[i]);
}

typedef string str;

template<typename T>
inline istream &operator >>(istream &in, vec<T> &a) {
	for (int i = 0; i < a.size(); i++) in >> a[i];
	return in;
}

template<typename T>
inline ostream &operator <<(ostream &out, vec<T> &a) {
	for (int i = 0; i < a.size(); i++) out << a[i];
	return out;
}

template<typename T, typename E>
inline istream &operator >>(istream &in, pair<T, E> &a) {
	in >> a.first >> a.second;
	return in;
}

template<typename T>
T gcd(T a, T b) {
	T c;
	while (b) {
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}

template<typename T>
T lcm(T a, T b) {
	return (a / gcd(a, b)) * b;
}

vector<int> prefix_function(string s) {
	int n = (int)s.length();
	vector<int> pi(n);
	for (int i = 1; i < n; ++i) {
		int j = pi[i - 1];
		while (j > 0 && s[i] != s[j])
			j = pi[j - 1];
		if (s[i] == s[j])  ++j;
		pi[i] = j;
	}
	return pi;
}

int binpow(int a, int n) {
	if (n == 0)
		return 1;
	if (n & 1)
		return binpow(a, n - 1) * a;
	else {
		int b = binpow(a, n >> 1);
		return b * b;
	}
}

vector<int> primes(int N) {
	vector<int> lp(N + 1);
	vector<int> pr;

	for (int i = 2; i <= N; ++i) {
		if (lp[i] == 0) {
			lp[i] = i;
			pr.push_back(i);
		}
		for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i*pr[j] <= N; ++j)
			lp[i * pr[j]] = pr[j];
	}
	return pr;
}

typedef vec<vi> matrix;

matrix operator*(matrix & a, matrix &b) {
	matrix m;
	int cx = a[0].size(), cy = b.size();
	m.resize(cx);
	for (int i = 0; i < cx; i++)
		m[i].resize(cy);
	for (int i = 0; i < cx; i++) {
		for (int j = 0; j < cy; j++) {
			int res = 0;
			for (int p = 0; p < b[0].size(); p++)
				res += a[i][p] * b[p][j];
			m[i][j] = res;
		}
	}
	return m;
}

int cnt = 0;
matrix pow(matrix &a, int b) {
	if (b == 1)
		return a;
	cnt++;
	if (b & 1)
	{
		auto r = pow(a, b - 1);
		r = r * a;
		return r;
	}
	else {
		auto r = pow(a, b / 2);
		r = r * r;
		return r;
	}
}

vector<vector<bool>> ok;
bool check(string s, string t) {
	bool f1 = false;
	for (int i = 0; i < s.size(); i++)
		if (s[i] == '1'&&t[i] == '0')
			f1 = true;
	bool f2 = false;
	for (int i = 0; i < s.size(); i++)
		if (s[i] == '0'&&t[i] == '1')
			f1 = true;

	return f1 && !f1;
}

inline void solve() {
	int t;
	cin >> t;
	while (t--) {
		int a, b;
		cin >> a >> b;
		cout << b * 2 << "\n";
	}
}

int32_t main() {
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	int k;
	cin >> k;
}


#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int n, x;
		cin >> n >> x;
		cout << 2 * x << endl;
	}
	return 0;
}


#include <iostream>
#define ll long long

using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--){
        ll n, x;
        cin >> n >> x;
        cout << x*2 << endl;
    }
    return 0;
}


include<bits/stdc++.h>
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
   int t;
  cin>>t;

  while(t--)
  {

    int d,e,n,m,col[n],row[n],a[n][n],c=0,j,i;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
      for(j=0;j<m;j++)
      {

        cin>>d;
        row[d]=j;
      }

    }
    for(i=0;i<m;i++)
    {
      for(j=0;j<n;j++)
      {
        cin>>e;
        row[e]=j;
      }
    }
    for(i=0;i<n*m;i++)
    {
      a[col[i]][row[i]]=i;
    }

    for(i=0;i<n;i++)
    {
      for(j=0;j<m;j++)
      {
        cout<<a[i]<<" ";
      }
      cout<<endl;
    }

  }
  return 0;

}


#include<bits/stdc++.h>
#define N  (509)
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int d,e,n,m,col[N*N],row[N*N],a[503][507],c=0,j,i;
int main()
{

   int t;
   cin>>t;
  while(t--)
  {

    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
      for(j=1;j<=m;j++)
      {

        cin>>d;
        row[d]=j;
      }

    }
    for(i=1;i<=m;i++)
    {
      for(j=1;j<=n;j++)
      {
        cin>>e;
        row[e]=j;
      }
    }

    for(i=1;i<=n*m;i++)
    {
     a[row[i]][col[i]]=i;
    }

    for(i=1;i<=n;i++)
    {
      for(j=1;j<=m;j++)
      {
        cout<<a[i][j]<<" ";
      }
      cout<<endl;
    }

  }
  return 0;

}


#include<bits/stdc++.h>
#define N (509)
#define optimize() ios_base ::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main()
{
  int d,e,n,m,col[n*n],row[n*n],a[n][n],c=0,j,i;
   int t;
  cin>>t;

  while(t--)
  {

    cin>>n>>m;
    for(i=1;i<=n;i++)
    {
      for(j=1;j<=m;j++)
      {

        cin>>d;
        row[d]=j;
      }

    }
    for(i=1;i<=m;i++)
    {
      for(j=1;j<=n;j++)
      {
        cin>>e;
        row[e]=j;
      }
    }

    for(i=1;i<=n*m;i++)
    {
      a[row[i]][col[i]]=i;
    }

    for(i=1;i<=n;i++)
    {
      for(j=1;j<=m;j++)
      {
        cout<<a[i][j]<<" ";
      }
      cout<<endl;
    }

  }
  return 0;

}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair <int, int> intpair;
#define longlongvector vector<long long>
#define llvector vector<long long>
#define intset set<int>
typedef long long ll;
#define pause system("pause")
#define overtwo >> 1
#define testcases int t; cin >> t; while (t--)
#define intvector vector<int>
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
#define size size()
#define mid ((l+r)>>1)
#define ever (;;)
#define isnotempty(s) !s.empty()
#define space " "
#define here cout<<"here"<<endl;
#define maxelement(x) *max_element(all(x));
#define setof(x,n) *next(x.begin(),n)
//void custominput() { freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); }
// deque --> insert or delete in the end or beginning O(1), access O(1), insert or delete in the end or beginning O(n)
// tips on binary search algorithm -> create mid outside the loop, check what you want, be careful on the boundries, check condition is very important
// tips on binary search algorithm -> try to simulate the search first, detecting that its binary search problem is fine, how to write it is the real thing
// count ... returns a number of time an element occurs in any array or vector  count ( a.begin, a.end, item )
// lowerebound --> first equal or greater than .... upper -> first greater than
// you can memeset a 2d char array....
// a pair is sorted according to its first
// sort(v.rbegin(), v.rend()-1);
// best practice strategy dealing with string creation or manipulation is to use a char array when needed.
// nth element -> sort array at some index with the previous <= it -> O(n) can be used if we don't care about sum of first k sorted elements
// loop through a map for (auto const& x : mp)
// if a%n = 0 & b%n = =0	THEN (a+b)%n = 0	and (a-b)%n = 0		as (a+b)%n = (a%n+b%n)%n = (0+0)%n = 0
int p[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
string yes[3] = { "YES", "Yes", "yes" }; string no[3] = { "NO", "No", "no" };
#define inf 0x3f3f3f3f
int countones(ll a) { int count = 0; while (a != 0) { count++; a = a & (a - 1); } return count; }
unsigned int reverse_bits(register unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
ll Fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,  10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578,  5702887, 9227465, 14930352, 24157817,39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903 };
ll sum(ll n) { return (n * (n + 1)) / 2; }
ll rangesum(ll a, ll b) { if (a <= 1) return sum(b); return sum(b) - sum(a - 1); }
bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); }
inline bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); }
ll lcm(ll a, ll b) { return (a*b) / gcd(a, b); }
bool palindrome(string a) { bool flag = 1;  int x = a.size; for (int i = 0; i < x / 2; i++) if (a[i] != a[x - i - 1]) { flag = 0; break; } return flag; }

const int maxnum = 505*505 +1;
int main()
{
	fast;
	int t;
	cin >> t;

	while (t--)
	{
		int n, m;
		cin >> n >> m;
		int r[maxnum], c[maxnum];
		int x;

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> x;
				c[x] = j;
			}
		}

		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> x;
				r[x] = j;
			}
		}
		int a[n+1][m+1];
		for (int i = 1; i <= n * m; i++)
		{
			a[r[i]][c[i]] = i;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++) cout << a[i][j] << " ";
			cout << "\n";
		}
	}

}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair <int, int> intpair;
#define longlongvector vector<long long>
#define llvector vector<long long>
#define intset set<int>
typedef long long ll;
#define pause system("pause")
#define overtwo >> 1
#define testcases int t; cin >> t; while (t--)
#define intvector vector<int>
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
#define size size()
#define mid ((l+r)>>1)
#define ever (;;)
#define isnotempty(s) !s.empty()
#define space " "
#define here cout<<"here"<<endl;
#define maxelement(x) *max_element(all(x));
#define setof(x,n) *next(x.begin(),n)
//void custominput() { freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); }
// deque --> insert or delete in the end or beginning O(1), access O(1), insert or delete in the end or beginning O(n)
// tips on binary search algorithm -> create mid outside the loop, check what you want, be careful on the boundries, check condition is very important
// tips on binary search algorithm -> try to simulate the search first, detecting that its binary search problem is fine, how to write it is the real thing
// count ... returns a number of time an element occurs in any array or vector  count ( a.begin, a.end, item )
// lowerebound --> first equal or greater than .... upper -> first greater than
// you can memeset a 2d char array....
// a pair is sorted according to its first
// sort(v.rbegin(), v.rend()-1);
// best practice strategy dealing with string creation or manipulation is to use a char array when needed.
// nth element -> sort array at some index with the previous <= it -> O(n) can be used if we don't care about sum of first k sorted elements
// loop through a map for (auto const& x : mp)
// if a%n = 0 & b%n = =0	THEN (a+b)%n = 0	and (a-b)%n = 0		as (a+b)%n = (a%n+b%n)%n = (0+0)%n = 0
int p[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
string yes[3] = { "YES", "Yes", "yes" }; string no[3] = { "NO", "No", "no" };
#define inf 0x3f3f3f3f
int countones(ll a) { int count = 0; while (a != 0) { count++; a = a & (a - 1); } return count; }
unsigned int reverse_bits(register unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
ll Fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,  10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578,  5702887, 9227465, 14930352, 24157817,39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903 };
ll sum(ll n) { return (n * (n + 1)) / 2; }
ll rangesum(ll a, ll b) { if (a <= 1) return sum(b); return sum(b) - sum(a - 1); }
bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); }
inline bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); }
ll lcm(ll a, ll b) { return (a*b) / gcd(a, b); }
bool palindrome(string a) { bool flag = 1;  int x = a.size; for (int i = 0; i < x / 2; i++) if (a[i] != a[x - i - 1]) { flag = 0; break; } return flag; }

const int maxnum = 505*505 +1;
int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n, m;
		cin >> n >> m;
		int r[maxnum], c[maxnum];
		int x;

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> x;
				c[x] = j;
			}
		}

		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> x;
				r[x] = j;
			}
		}
		int a[n][m];
		for (int i = 1; i <= n * m; i++)
		{
			a[r[i]][c[i]] = i;
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++) cout << a[i][j] << " ";
			cout << "\n";
		}
	}

}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair <int, int> intpair;
#define longlongvector vector<long long>
#define llvector vector<long long>
#define intset set<int>
typedef long long ll;
#define pause system("pause")
#define overtwo >> 1
#define testcases int t; cin >> t; while (t--)
#define intvector vector<int>
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
#define size size()
#define mid ((l+r)>>1)
#define ever (;;)
#define isnotempty(s) !s.empty()
#define space " "
#define here cout<<"here"<<endl;
#define maxelement(x) *max_element(all(x));
#define setof(x,n) *next(x.begin(),n)
//void custominput() { freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); }
// deque --> insert or delete in the end or beginning O(1), access O(1), insert or delete in the end or beginning O(n)
// tips on binary search algorithm -> create mid outside the loop, check what you want, be careful on the boundries, check condition is very important
// tips on binary search algorithm -> try to simulate the search first, detecting that its binary search problem is fine, how to write it is the real thing
// count ... returns a number of time an element occurs in any array or vector  count ( a.begin, a.end, item )
// lowerebound --> first equal or greater than .... upper -> first greater than
// you can memeset a 2d char array....
// a pair is sorted according to its first
// sort(v.rbegin(), v.rend()-1);
// best practice strategy dealing with string creation or manipulation is to use a char array when needed.
// nth element -> sort array at some index with the previous <= it -> O(n) can be used if we don't care about sum of first k sorted elements
// loop through a map for (auto const& x : mp)
// if a%n = 0 & b%n = =0	THEN (a+b)%n = 0	and (a-b)%n = 0		as (a+b)%n = (a%n+b%n)%n = (0+0)%n = 0
int p[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
string yes[3] = { "YES", "Yes", "yes" }; string no[3] = { "NO", "No", "no" };
#define inf 0x3f3f3f3f
int countones(ll a) { int count = 0; while (a != 0) { count++; a = a & (a - 1); } return count; }
unsigned int reverse_bits(register unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
ll Fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,  10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578,  5702887, 9227465, 14930352, 24157817,39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903 };
ll sum(ll n) { return (n * (n + 1)) / 2; }
ll rangesum(ll a, ll b) { if (a <= 1) return sum(b); return sum(b) - sum(a - 1); }
bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); }
inline bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); }
ll lcm(ll a, ll b) { return (a*b) / gcd(a, b); }
bool palindrome(string a) { bool flag = 1;  int x = a.size; for (int i = 0; i < x / 2; i++) if (a[i] != a[x - i - 1]) { flag = 0; break; } return flag; }

const int maxnum = 505*505 +1;
int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n, m;
		cin >> n >> m;
		int r[maxnum], c[maxnum];
		int x;

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> x;
				c[x] = j;
			}
		}

		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> x;
				r[x] = j;
			}
		}
		int a[n+1][m+1];
		for (int i = 1; i <= n * m; i++)
		{
			a[r[i]][c[i]] = i;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++) cout << a[i][j] << " ";
			cout << "\n";
		}
	}

}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair <int, int> intpair;
#define longlongvector vector<long long>
#define llvector vector<long long>
#define intset set<int>
typedef long long ll;
#define pause system("pause")
#define overtwo >> 1
#define testcases int t; cin >> t; while (t--)
#define intvector vector<int>
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
#define size size()
#define mid ((l+r)>>1)
#define ever (;;)
#define isnotempty(s) !s.empty()
#define space " "
#define here cout<<"here"<<endl;
#define maxelement(x) *max_element(all(x));
#define setof(x,n) *next(x.begin(),n)
//void custominput() { freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); }
// deque --> insert or delete in the end or beginning O(1), access O(1), insert or delete in the end or beginning O(n)
// tips on binary search algorithm -> create mid outside the loop, check what you want, be careful on the boundries, check condition is very important
// tips on binary search algorithm -> try to simulate the search first, detecting that its binary search problem is fine, how to write it is the real thing
// count ... returns a number of time an element occurs in any array or vector  count ( a.begin, a.end, item )
// lowerebound --> first equal or greater than .... upper -> first greater than
// you can memeset a 2d char array....
// a pair is sorted according to its first
// sort(v.rbegin(), v.rend()-1);
// best practice strategy dealing with string creation or manipulation is to use a char array when needed.
// nth element -> sort array at some index with the previous <= it -> O(n) can be used if we don't care about sum of first k sorted elements
// loop through a map for (auto const& x : mp)
// if a%n = 0 & b%n = =0	THEN (a+b)%n = 0	and (a-b)%n = 0		as (a+b)%n = (a%n+b%n)%n = (0+0)%n = 0
int p[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
string yes[3] = { "YES", "Yes", "yes" }; string no[3] = { "NO", "No", "no" };
#define inf 0x3f3f3f3f
int countones(ll a) { int count = 0; while (a != 0) { count++; a = a & (a - 1); } return count; }
unsigned int reverse_bits(register unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
ll Fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,  10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578,  5702887, 9227465, 14930352, 24157817,39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903 };
ll sum(ll n) { return (n * (n + 1)) / 2; }
ll rangesum(ll a, ll b) { if (a <= 1) return sum(b); return sum(b) - sum(a - 1); }
bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); }
inline bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); }
ll lcm(ll a, ll b) { return (a*b) / gcd(a, b); }
bool palindrome(string a) { bool flag = 1;  int x = a.size; for (int i = 0; i < x / 2; i++) if (a[i] != a[x - i - 1]) { flag = 0; break; } return flag; }

const int maxnum = 505*505 +1;
int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n, m;
		cin >> n >> m;
		int r[maxnum], c[maxnum];
		int x;

		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> x;
				c[x] = j;
			}
		}

		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				cin >> x;
				r[x] = j;
			}
		}
		int a[n][m];
		for (int i = 1; i <= n * m; i++)
		{
			a[r[i]][c[i]] = i;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++) cout << a[i][j] << " ";
			cout << "\n";
		}
	}

}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef pair <int, int> intpair;
#define longlongvector vector<long long>
#define llvector vector<long long>
#define intset set<int>
typedef long long ll;
#define pause system("pause")
#define overtwo >> 1
#define testcases int t; cin >> t; while (t--)
#define intvector vector<int>
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) x.begin(),x.end()
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
#define size size()
#define mid ((l+r)>>1)
#define ever (;;)
#define isnotempty(s) !s.empty()
#define space " "
#define here cout<<"here"<<endl;
#define maxelement(x) *max_element(all(x));
#define setof(x,n) *next(x.begin(),n)
//void custominput() { freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); }
// deque --> insert or delete in the end or beginning O(1), access O(1), insert or delete in the end or beginning O(n)
// tips on binary search algorithm -> create mid outside the loop, check what you want, be careful on the boundries, check condition is very important
// tips on binary search algorithm -> try to simulate the search first, detecting that its binary search problem is fine, how to write it is the real thing
// count ... returns a number of time an element occurs in any array or vector  count ( a.begin, a.end, item )
// lowerebound --> first equal or greater than .... upper -> first greater than
// you can memeset a 2d char array....
// a pair is sorted according to its first
// sort(v.rbegin(), v.rend()-1);
// best practice strategy dealing with string creation or manipulation is to use a char array when needed.
// nth element -> sort array at some index with the previous <= it -> O(n) can be used if we don't care about sum of first k sorted elements
// loop through a map for (auto const& x : mp)
// if a%n = 0 & b%n = =0	THEN (a+b)%n = 0	and (a-b)%n = 0		as (a+b)%n = (a%n+b%n)%n = (0+0)%n = 0
int p[11] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
string yes[3] = { "YES", "Yes", "yes" }; string no[3] = { "NO", "No", "no" };
#define inf 0x3f3f3f3f
int countones(ll a) { int count = 0; while (a != 0) { count++; a = a & (a - 1); } return count; }
unsigned int reverse_bits(register unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
ll Fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,  10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578,  5702887, 9227465, 14930352, 24157817,39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903 };
ll sum(ll n) { return (n * (n + 1)) / 2; }
ll rangesum(ll a, ll b) { if (a <= 1) return sum(b); return sum(b) - sum(a - 1); }
bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); }
inline bool cmp(ll a, ll b) { return a > b; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); }
ll lcm(ll a, ll b) { return (a*b) / gcd(a, b); }
bool palindrome(string a) { bool flag = 1;  int x = a.size; for (int i = 0; i < x / 2; i++) if (a[i] != a[x - i - 1]) { flag = 0; break; } return flag; }

const int maxnum = 505*505 +1;
int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n, m;
		cin >> n >> m;
		int r[maxnum], c[maxnum];
		int x;

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				cin >> x;
				c[x] = j;
			}
		}

		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				cin >> x;
				r[x] = j;
			}
		}
		int a[n][m];
		for (int i = 1; i <= n * m; i++)
		{
			a[r[i]][c[i]] = i;
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++) cout << a[i][j] << " ";
			cout << "\n";
		}
	}

}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;

        int a[n][m],b[m][n];

        for(int i=0; i<n; i++)
        {
            for(int j=0;j<m; j++)
            {
                cin>>a[i][j];
            }
        }

         for(int i=0; i<m; i++)
        {
            for(int j=0;j<n; j++)
            {
                cin>>b[i][j];
            }
        }
         for(int i=0; i<n; i++)
         {
             for(int j=0; j<n; j++)
             {
                 if(b[0][i]==a[j][0])
                 {
                     for(int k=0; k<m; k++)
                     {
                         cout<<a[j][k]<<" ";
                     }
                     cout<<endl;
                 }
             }
         }
    }
    return 0;
}


#include<map>
#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
    int n,m;
    cin>>n>>m;
    int a[n+5][m+5],b[m+5][n+5];
    int p1[n*m+5],p2[n*m+5];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>a[i][j];
            p1[a[i][j]]=j;
        }
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>b[i][j];
              p2[b[i][j]]=j;
        }
    }
    map<pair<int,int>,int> ma;
    for(int i=1;i<=n*m;i++)
    {
        ma[{p2[i],p1[i]}]=i;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<ma[{i,j}]<<" ";
        }
        cout<<endl;
    }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;

        int a[n][m],b[m][n];

        for(int i=0; i<n; i++)
        {
            for(int j=0;j<m; j++)
            {
                cin>>a[i][j];
            }
        }

         for(int i=0; i<m; i++)
        {
            for(int j=0;j<n; j++)
            {
                cin>>b[i][j];
            }
        }

           for(int i=0; i<n; i++)
        {
            for(int j=0;j<m; j++)
            {
                cout<<b[j][i]<<" ";
            }
            cout<<"\n";
        }

    }
}


#include <bits/stdc++.h>

using ll = long long;

using namespace std;

const int N = 510;
int a[N][N];
int b[N][N];
int p[N * N];

void solve()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            p[a[i][j]] = i;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> b[i][j];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << a[p[b[1][i]]][j] << ' ';
        cout << '\n';
    }
}

int main(int argc, char** argv)
{
   ios_base::sync_with_stdio(false);
   int t;
   cin >> t;
   while (t--) solve();
   return 0;
}


#include <iostream>
#include <vector>

using table = std::vector<std::vector<int>>;

int main(int argc, char** argv)
{
    int t;
    std::cin >> t;
    std::vector<table> solutions(t);
    for (auto& sol : solutions)
    {
        int n, m;
        std::cin >> n >> m;
        table a(n);
        sol.resize(n);
        for (int i = 0; i < n; i++)
        {
            a[i].resize(m);
            for (int& num : a[i]) std::cin >> num;
        }
        std::vector<int> col(n);
        for (int& num : col) std::cin >> num;
        for (int i = 0; i < n; i++)
        {
            bool finded = false;
            for (int j = 0; j < m; j++)
                if (a[i][j] == col[0])
                {
                    for (int x = 0; x < n; x++)
                        for (int y = 0; y < n; y++)
                            if (a[x][0] == col[y])
                                sol[y] = a[x];
                    finded = true;
                    break;
                }
            if (finded) break;
        }
        for (int i = 1; i < m; i++)
            for (int j = 0; j < n; j++)
            {
                int tmp;
                std::cin >> tmp;
            }
    }
    for (auto& sol : solutions)
        for (auto& row : sol)
        {
            for (auto& col : row)
                std::cout << col << ' ';
            std::cout << '\n';
        }
    return 0;
}


#include <iostream>
#include <vector>

int main(int argc, char** argv)
{
    int t;
    std::cin >> t;
    using table = std::vector<std::vector<int>>;
    std::vector<table> solutions(t);
    for (auto& sol : solutions)
    {
        int n, m;
        std::cin >> n >> m;
        table a(n);
        std::vector<int> hash(n * m);
        sol.resize(n);
        for (int i = 0; i < n; i++)
        {
            a[i].resize(m);
            for (int& num : a[i]) std::cin >> num;
            hash[a[i][0] - 1] = i;
        }
        for (int i = 0; i < n; i++)
        {
            int index;
            std::cin >> index;
            sol[i] = std::move(a[hash[index - 1]]);
        }
        for (int i = 1; i < m; i++)
            for (int j = 0; j < n; j++)
            {
                int tmp;
                std::cin >> tmp;
            }
    }
    for (auto& sol : solutions)
        for (auto& row : sol)
        {
            for (auto& col : row)
                std::cout << col << ' ';
            std::cout << '\n';
        }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;

using namespace std;

const int N = 510;
int a[N][N];
int b[N][N];
int p[N * N];

void solve()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1;i <= n; i++){
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            p[ a[i][j] ] = i;
        }
    }
    for (int i = 1;i <= m; i++){
        for (int j = 1; j<=n; j++)
            cin >> b[i][j];
    }
    for (int i = 1;i <= n; i++){
        for (int j = 1; j <= m; j++)
            cout << a[ p[ b[1][i] ] ][j] << " ";
        cout<<'\n';
    }
}

int main(int argc, char** argv)
{
   ios_base::sync_with_stdio(false);
   int t;
   cin >> t;
   while (t--) solve();
   return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<fstream>
#include <cstring>

using namespace std;

void task(){
    int n, m;
    cin>> n>>m;
    vector<vector<int>> ns(n),ms(m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int x;
            cin>> x;
            ns[i].push_back(x);
        }
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            int x;
            cin>> x;
            ms[i].push_back(x);
        }
    }
    vector<vector<int>> ans;

    for (auto elem : ms[0]) {
        for (int i = 0; i < n; ++i) {
            if (ns[i][0] != 0){
                for (int j = 0; j < m; ++j) {
                    if (ns[i][j] == elem){

                        ans.push_back(ns[i]);
                        ns[i][0] = 0;
                    }
                }
            }
        }
    }

    for (auto el : ans) {
        for (int i = 0; i < m; ++i) {
                cout << el[i] << " ";
        }
        cout<<endl;
    }
}

int main() {
    // fin("input.txt");
    //ofstream fout("output.txt");
    int t;
    cin >> t;

    for (int i = 0; i < t; ++i) {
        task();
    }
    //fin.close();
    //fout.close();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

int main() {
	int t;
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> t;
	while (t-- > 0) {
		int n, m;

		cin >> n >> m;
		vector<vector<int>> a(n, vector<int>(m));
		vector<vector<int>> b(n, vector<int>(m));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				cin >> b[j][i];
			}
		}
		int x, y;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (b[j][i] == a[0][0]) {
					x = i, y = j;
					break;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (a[j][0] == b[i][x]) {
					for (int q = 0; q < m; q++) {
						cout << a[j][q] << ' ';
					}
					cout << endl;
					continue;
				}
			}
		}

	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <cmath>
#include <string>
#include <random>
using namespace std;
const long long mod = 1000000000 + 7;
int a[502][502], b[502][502], I[502 * 502], J[502 * 502], c[502][502];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				cin >> a[i][j];
				J[a[i][j]] = j;
			}
		for (int j = 0; j < m; j++)
			for (int i = 0; i < n; i++)
			{
				cin >> b[i][j];
				I[b[i][j]] = i;
			}
		for (int i = 1; i <= n*m; i++)
		{
			c[I[i]][J[i]] = i;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cout << c[i][j] << ' ';
			cout << '\n';
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main()
{

	int t;
	cin >> t;
	while (t--)
	{
		int n,m,f=-1;
		cin >> n>>m;
		vector <vector<int>> a(n);
		unordered_map <int, int> mp;
		for (int i = 0; i < n; ++i)
		{
			vector<int> x(m);
			for(int j=0;j<m;++j)
				cin >> x[j];
			a[i] = x;
			mp[x[0]] = i;
		}
		vector <vector<int>> b(m);

		for (int i = 0; i < m; ++i)
		{
			vector<int> x(n);
			for (int j = 0; j < n; ++j)
				cin >> x[j];
			b[i] = x;
			if (mp.find(x[0])!=mp.end())
				f = i;
		}
		for (auto i : b[f])
		{
			for (int j = 0; j < m; ++j)
				cout << a[mp[i]][j]<< ' ';
			cout << '\n';
		}

	}
}


#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main()
{

	int t;
	cin >> t;
	while (t--)
	{
		int n,m,f=-1;
		cin >> n>>m;
		vector <vector<int>> a(n);
		unordered_map <int, int> mp;
		for (int i = 0; i < n; ++i)
		{
			vector<int> x(m);
			for(int j=0;j<m;++j)
				cin >> x[j];
			a[i] = x;
			mp[x[0]] = i;
		}
		vector <vector<int>> b(m);

		for (int i = 0; i < m; ++i)
		{
			vector<int> x(n);
			for (int j = 0; j < n; ++j)
				cin >> x[j];
			b[i] = x;
			if (mp[x[0]] > 0 && mp[x[0]] < n * m + 1)
				f = i;
		}
		for (auto i : b[f])
		{
			for (int j = 0; j < m; ++j)
				cout << a[mp[i]][j]<< ' ';
			cout << '\n';
		}

	}
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <deque>
#include <algorithm>
#include <cmath>

#define ll long long int
#define db double

using namespace std;

int main () {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int _;
	cin >> _;
	while (_--) {
		ll n, m;
		cin >> n >> m;
		vector <vector <ll>> h(n), v(m);
		set <ll> f1;
		map <ll, ll> por;
		ll need;

		ll mass;

		for (int i = 0; i < n; ++i) {
			h[i].resize(m);
			for (int j = 0; j < m; ++j) cin >> h[i][j];
			f1.insert(h[i][0]);
			por[h[i][0]] = i;
		}
		for (int i = 0; i < m; ++i) {
			v[i].resize(n);
			for (int j = 0; j < n; ++j) cin >> v[i][j];
			if (f1.find(v[i][0]) != f1.end()) {
				need = v[i][0];
				mass = i;
			}
		}

		for (int i = 0; i < v[mass].size(); ++i) {
			for (int j = 0; j < h[por[v[mass][0]]].size(); ++j) {
				cout << h[por[v[mass][i]]][j] << " ";
			}
			cout << "\n";
		}

	}
 return 0;
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <deque>
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0)
#define F first
#define S second
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pb push_back
#define sqr(x) (x)*(x)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
	int t, n, m;
	cin >> t;
	while (t--) {
		cin >> n >> m;
		vector<vector<int>> hor(n, vector<int>(m));
		vector<vector<int>> ver(m, vector<int>(n));
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				cin >> hor[i][j];
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				cin >> ver[i][j];
		map<int, int> beginnings;
		for (int i = 0; i < n; i++)
			beginnings[hor[i][0]] = i;
		vector<int> res(n);
		int first_ver = -1;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (ver[i][j] == hor[0][0]) {
					first_ver = i;
					break;
				}
			}
			if (first_ver != -1) break;
		}
		for (int i = 0; i < n; i++)
			res[i] = beginnings[ver[first_ver][i]];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cout << hor[res[i]][j] << " ";
			cout << endl;
		}
	}
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		vector <vector <int>> a(m, vector <int>(n)), b(m, vector <int> (n)), a1(n, vector <int> (m));
		set <int> x;
		map <int, int> find;
		vector <int> ans;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[j][i];
				a1[i][j] = a[j][i];
				if (j == 0) {
					x.insert(a[j][i]);
					find[a[j][i]] = i;
				}
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				cin >> b[i][j];
			}
		}
		int ind = -1;
		for (int i = 0; i < m; i++) {
			if (x.find(b[i][0]) != x.end()) {
				ind = i;
				break;
			}
		}
		for (int i = 0; i < n; i++) {
			ans.push_back(find[b[ind][i]]);
		}
		for (auto i : ans) {
			for (int j = 0; j < m; j++) {
				cout << a1[i][j] << ' ';
			}
			cout << "\n";
		}
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<vector>
#include<unordered_map>

using namespace std;
int main(){
	int t, n, m;
	cin >> t;
	for (int e = 0; e < t; e++){
		cin >> n >> m;
		vector<vector<int>> arr(n, vector<int>(m));
		vector<vector<int>> ans(n, vector<int>(m));
		unordered_map<int, vector<int>> aq;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				cin >> arr[i][j];
			}
			aq[arr[i][0]] = arr[i];
		}
		int x;
		for (int i = 0; i < m; i++){
			for (int j = 0; j < n; j++){
				cin >> x;
				if (aq.find(x) != aq.end())
					ans[j] = aq[x];
			}
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				cout << ans[i][j] << ' ';
			}
			cout << endl;
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
typedef long long ll;
typedef unsigned long long ull;

#include<cstdio>
#include<cmath>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#include<set>
using namespace std;

int main()
{

	ll t, n, m, x;
	vector<vector<int>> arr;
	vector<int>d;
	vector<int> indexes;
	bool flag = true;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		cin >> n >> m;
		for (int j = 0; j < n; j++)
		{

			for (int k = 0; k < m; k++)
			{
				cin >> x;
				d.push_back(x);
			}
			arr.push_back(d);
			d.clear();
		}

		for (int h = 0; h < n; h++)
		{
			cin >> x;
			flag = true;
			for (int j = 0; j < n; j++)
			{

				for (int k = 0; k < m; k++)
				{
					if (arr[j][k] == x)
					{
						indexes.push_back(j);
						flag = false;
						break;
					}
					if (!flag)break;
				}
			}
		}
		for (int h = 0; h < (m - 1) * n; h++)cin >> x;
		for (int h = 0; h < indexes.size(); h++)
		{
			for (int j = 0; j < m; j++)
			{
				cout << arr[indexes[h]][j] << ' ';
			}
			cout << endl;
		}
		d.clear();
		indexes.clear();
		arr.clear();
	}

}


#include <iostream>
#include <string>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		int a[501][501];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> a[i][j];
			}
		}
		int b[550];
		bool d = false;
		for (int j = 1; j <= m; j++) {
			for (int i = 1; i <= n; i++) {
				int at;
				if (d) { cin >> at; }
				else { cin >> b[i]; }
				if (b[i] == a[1][1]) {
					d = true;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int k = 1; k <= n; k++) {
				if (b[i] == a[k][1]) {
					for (int j = 1; j <= m; j++) {
						cout << a[k][j] << " ";
					}
					cout << endl;
					break;
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		int a[501][501];
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> a[i][j];
			}
		}
		int b[550];
		bool d = false;
		int p = 1000;
		for (int j = 1; j <= m; j++) {
			for (int i = 1; i <= n; i++) {
				int at;
				if (d && j>p) { cin >> at; }
				else { cin >> b[i]; }
				if (b[i] == a[1][1] && d!=true) {
					d = true;
					p = j;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int k = 1; k <= n; k++) {
				if (b[i] == a[k][1]) {
					for (int j = 1; j <= m; j++) {
						cout << a[k][j] << " ";
					}
					cout << endl;
					break;
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main() {
    int a;
    cin >> a;
    for (int i = 0; i < a; i++) {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> rows(n);
        vector<vector<int>> cols(m);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int k;
                cin >> k;
                rows[i].push_back(k);
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int k;
                cin >> k;
                cols[i].push_back(k);
            }
        }
        if (n <= m) {
            for (int k = 0; k < n; k++) {
                for (int i = 0; i < m; i++) {
                    for (int j = 0; j < n; j++) {
                        if (cols[i][k] == rows[j][k]) {
                            for (int d = 0; d < m; d++) {
                                cout << rows[j][d] << " ";
                            }
                            cout << endl;
                            break;
                        }
                    }
                }
            }
        } else {
            int firstcol, firstrow;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (cols[i][0] == rows[j][0]) {
                        for (int d = 0; d < m; d++) {
                            cout << rows[j][d] << " ";
                        }
                        cout << endl;
                        firstcol = i;
                        firstrow = j;
                        break;
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (j == firstrow) {
                        continue;
                    } else {
                        if (rows[j][0] == cols[firstcol][i]) {
                            for (int d = 0; d < m; d++) {
                                cout << rows[j][d] << " ";
                            }
                            cout << endl;
                        }
                    }
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		//vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		int save;
		for (int j = 0; j < mapCollons.size(); j++)
		{
			for (int k = 0; k < mapCollons[0].size(); k++)
			{
				if (mapRows[0][0] == mapCollons[j][k])
				{
					save = j;
				}
			}
		}
		for (int j = 0; j < mapCollons[save].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[save][j] == mapRows[k][0] || mapCollons[save][j] == mapRows[k][collons-1])
					{
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							cout << mapRows[k][x] << " ";
							checked[k] = true;
						}
						cout << '\n';
					}
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		//vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0]  || mapCollons[collons-1][j] == mapRows[k][0])
					{
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							cout << mapRows[k][x] << " ";
							checked[k] = true;
						}
						cout << '\n';
					}
					else if (mapCollons[collons - 1][j] == mapRows[k][collons - 1] || mapCollons[0][j] == mapRows[k][collons - 1])
					{
						for (int x = mapRows[k].size() - 1; x >= 0; x++)
						{
							cout << mapRows[k][x] << " ";
							checked[k] = true;
						}
						cout << '\n';
					}
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] && index == mn)
					{
						index = k;
					}
					else if (mapCollons[0][j] == mapRows[k][0] && index != mn)
					{
						int first, second;
						int check = -1;
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							if (mapRows[index][x] != mapRows[k][x])
							{
								first = mapRows[index][x];
								second = mapRows[k][x];
								check = x;
								break;
							}
						}
						if (check != -1)
						{
							if (mapCollons[check][j] == first)
							{

							}
							else
							{
								index = k;
							}
						}
					}
				}
			}
			for (int x = 0; x < mapRows[index].size(); x++)
			{
				cout << mapRows[index][x] << " ";
			}
			cout << '\n';
			checked[index] = true;
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		//vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{

				if (checked[k] != true)
				{
					if ((mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons - 1] || mapCollons[collons - 1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons - 1]) && index == mn)
					{
						index = k;
					}
					else if ((mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons - 1] || mapCollons[collons - 1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons - 1]) && index != mn)
					{
						int first, second;
						int check = -1;
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							if (mapRows[index][x] != mapRows[k][x])
							{
								first = mapRows[index][x];
								second = mapRows[k][x];
								check = x;
								break;
							}
						}
						if (check != -1)
						{
							if (mapCollons[check][j] == second || mapCollons[collons-1-check][j] == second)
							{
								index = k;
							}
						}
					}
				}
			}
			for (int x = 0; x < mapRows[index].size(); x++)
			{
				cout << mapRows[index][x] << " ";
			}
			cout << '\n';
			checked[index] = true;
				/*if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons-1] || mapCollons[collons-1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons-1])
					{
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							cout << mapRows[k][x] << " ";
							checked[k] = true;
						}
						cout << '\n';
					}
				}*/
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons-1])
					{
						index = k;
					}
				}
			}
			for (int x = 0; x < mapRows[index].size(); x++)
			{
				cout << mapRows[index][x] << " ";
			}
			cout << '\n';
			checked[index] = true;
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		//vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{

				if (checked[k] != true)
				{
					if ((mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons - 1] || mapCollons[collons - 1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons - 1]) && index == mn)
					{
						index = k;
					}
					else if ((mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons - 1] || mapCollons[collons - 1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons - 1]) && index != mn)
					{
						int first, second;
						int check = -1;
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							if (mapRows[index][x] != mapRows[k][x])
							{
								first = mapRows[index][x];
								second = mapRows[k][x];
								check = x;
								break;
							}
						}
						if (check != -1)
						{
							if (mapCollons[check][j] == first)
							{

							}
							else
							{
								index = k;
							}
						}
					}
				}
			}
			for (int x = 0; x < mapRows[index].size(); x++)
			{
				cout << mapRows[index][x] << " ";
			}
			cout << '\n';
			checked[index] = true;
				/*if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons-1] || mapCollons[collons-1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons-1])
					{
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							cout << mapRows[k][x] << " ";
							checked[k] = true;
						}
						cout << '\n';
					}
				}*/
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons-1] || mapCollons[collons-1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons-1])
					{
						index = k;
					}
				}
			}
			for (int x = 0; x < mapRows[index].size(); x++)
			{
				cout << mapRows[index][x] << " ";
			}
			cout << '\n';
			checked[index] = true;
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		//vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] || mapCollons[0][j] == mapRows[k][collons-1] || mapCollons[collons-1][j] == mapRows[k][0] || mapCollons[collons - 1][j] == mapRows[k][collons-1])
					{
						for (int x = 0; x < mapRows[k].size(); x++)
						{
							cout << mapRows[k][x] << " ";
							checked[k] = true;
						}
						cout << '\n';
					}
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define int long long

using namespace std;

constexpr const int mn = 10e10;

signed main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int rows, collons;
		cin >> rows >> collons;

		vector<vector<int>> map(rows, vector<int>(collons));
		vector<vector<int>>  mapRows(rows, vector<int>(collons));
		vector<bool> checked(rows);
		for (int j = 0; j < rows; j++)
		{
			for (int k = 0; k < collons; k++)
			{
				cin >> mapRows[j][k];
			}
		}
		vector<vector<int>>  mapCollons(collons, vector<int>(rows));
		for (int j = 0; j < collons; j++)
		{
			for (int k = 0; k < rows; k++)
			{
				cin >> mapCollons[j][k];
			}
		}
		for (int j = 0; j < mapCollons[0].size(); j++)
		{
			//int count = 0;
			int index = mn;
			for (int k = 0; k < mapRows.size(); k++)
			{
				if (checked[k] != true)
				{
					if (mapCollons[0][j] == mapRows[k][0] && index == mn)
					{
						index = k;
					}
				}
			}
			for (int x = 0; x < mapRows[index].size(); x++)
			{
				cout << mapRows[index][x] << " ";
			}
			cout << '\n';
			checked[index] = true;
		}
	}
	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>

using namespace std;

int main()
{
    int t; cin >> t;

    for (int k = 0; k < t; k++) {
        int n, m; cin >> n >> m;
        vector< vector<int> > matr(n);
        for (int i = 0; i < n; i++) {
            matr[i].resize(m);
            for (int j = 0; j < m; j++) cin >> matr[i][j];
        }

        for (int i = 0; i < m; i++) {
            int a; cin >> a;
            bool f = false; int num;
            for (int j = 0; j < n; j++) {
                if (matr[j][0] == a) {
                    f = true;
                    num = j;
                    break;
                }
            }

            if (f) {
                swap(matr[num], matr[0]);
                int last = 1;

                for (int e = 0; e < n - 1; e++) {
                    cin >> a;
                    for (int j = last; j < n; j++) {

                        if (matr[j][0] == a) {
                            swap(matr[j], matr[last]);
                            last++;
                            continue;
                        }
                    }
                }
            }
            else {
                for (int j = 0; j < n - 1; j++) {
                    cin >> a;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) cout << matr[i][j] << ' ';
            cout << endl;
        }
    }

    //system("pause");
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define rep(x) for (int i = 0; i < x; ++i)
#define rep2(x) for (int j = 0; j < x; ++j)
#define rep_(x) for (int _ = 0; _ < x; ++_)
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define mp(a, b) make_pair((a), (b))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <random>
#include <time.h>
#include <cmath>
#include <math.h>
using namespace std;

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int t;
	cin >> t;
	rep_(t)
	{
		map<int, vector<int> > ma;
		int n, m, tmp;
		cin >> n >> m;
		rep(n)
		{
			vector<int> a;
			rep2(m)
			{
				cin >> tmp;
				a.push_back(tmp);
			}
			ma[a[0]] = a;
		}
		vector<int> res;
		rep(m)
		{
			vector<int> a;
			rep2(n)
			{
				cin >> tmp;
				a.push_back(tmp);
			}
			if (ma.find(a[0]) != ma.end())
				res = a;
		}
		rep(res.size())
		{
			rep2(ma[res[i]].size())
				cout << ma[res[i]][j] << ' ';
			cout << '\n';
		}
	}
	return 0;
}


#include <vector>
#include <math.h>
#include <set>
#include <iostream>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
int main(){

    int T;
    cin >> T;

    forn(i,T){
        int n,m;
        cin >> n >> m;
        vector<vector<int>> matrix(n,vector<int>(m));
        vector<vector<int>> rows(m,vector<int>(n));

        forn(j,n){
            forn(k,m){
                cin >> matrix[j][k];
            }
        }

        forn(j,m){
            forn(k,n){
                cin >> rows[j][k];
            }
        }

        auto findRow = [&matrix,&n,&m](int i,int num){
            forn(j,n){
                forn(k,m){
                    if(matrix[j][k] == num){
                        swap(matrix[j],matrix[i]);
                        return;
                    }
                }
            }
        };
        forn(i,n){
            findRow(i,rows[0][i]);
        }

        forn(j,n){
            forn(k,m){
                cout << matrix[j][k] << " ";
            }
            cout << endl;
        }
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<climits>
#include<string>

using namespace std;

void res()
{
	int n, m;
	cin >> n >> m;
	map<int, vector<int>> test;
	for (int i = 0;i < n;i++)
	{
		int first;
		cin >> first;
		test[first].resize(m);
		test[first][0] = first;
		for (int j = 1;j < m;j++)
		{
			cin >> test[first][j];
		}
	}
	for (int i = 0;i < m;i++)
	{
		int a;
		cin >> a;
		if (test[a].size() != 0)
		{
			for (auto now : test[a])
			{
				cout << now << " ";
			}
			cout << '\n';
			for (int j = 1;j < n;j++)
			{
				int a;
				cin >> a;
				for (auto now : test[a])
				{
					cout << now << " ";
				}
				cout << '\n';
			}
		}
		else {
			/*	string str;
				getline(cin, str);*/
			cin.ignore(INT_MAX, '\n');
			continue;
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	/*
	cin.tie(0)->sync_with_stdio(0);
	cout.tie(0)->sync_with_stdio(0);
	*/
	int t;
	cin >> t;
	for (int i = 0;i < t;i++)
	{
		res();
	}
}


#include <iostream>
#include <vector>
#include<algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int u = 0; u < t; u++)
    {
        int n, m;
        cin >> n >> m;
        vector<vector<int> > arrStrings(n, vector<int>(m));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> arrStrings[i][j];
            }

        }
        vector<vector<int>> arrColons(m, vector<int>(n));
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cin >> arrColons[i][j];
            }

        }
        vector<vector<int>> res(n, vector<int>(m));
        if (arrColons.size() > 1)
        {
            for (int i = 0; i < n; i++)
            {
                for (int y = 0; y < m; y++)
                {
                    for (int j = 0; j < n; j++)
                    {

                        if (arrStrings[i][0] == arrColons[y][j])
                        {
                            res[i] = arrStrings[j];
                        }
                        if (res[i][0] != 0)
                        {
                            y = m + 1;
                            break;
                        }

                    }
                }
                if (res[n - 1][m - 1] != 0)
                {
                    break;
                }

            }
        }
        else
        {
            for (int i = 0; i < n; i++)
            {

                res[i][0] = arrColons[0][i];

            }
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cout << res[i][j] << " ";
            }
            cout << endl;
        }
    }
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main(int argc, char* argv[])
{
	int t;
	cin >> t;
	for (int t1 = 0; t1 < t; ++t1)
	{
		int n, m;
		cin >> n >> m;
		map<int, int> d;
		vector<vector<int> > a(n, vector<int>(m));
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				cin >> a[i][j];
				d[a[i][j]] = i;
			}
		}
		vector<vector<int> > a1(m, vector<int>(n));
		for (int j = 0; j < m; ++j)
		{
			for (int i = 0; i < n; ++i)
			{
				cin >> a1[j][i];
			}
		}
		for (int i = 0; i < n; ++i)
		{
			int idx = d[a1[0][i]];
			for (int j = 0; j < m; ++j)
			{
				cout << a[idx][j] << ' ';
			}
			cout << endl;
		}
	}

}


#include <cstdio>
#include <vector>
#include <map>
#include <set>

using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        map<int, int> qa;

        vector<vector<int>> a(n, vector<int>(m, 0));
        for (int y = 0; y < n; y++)
            for (int k = 0; k < m; k++)
            {
                int e;
                scanf("%d", &e);
                if (k == 0)
                    qa[e] = y;
                a[y][k] = e;
            }
        vector<set<int>> sb(m);
        vector<vector<int>> b(m, vector<int>(n, 0));

        for (int x = 0; x < m; x++)
            for (int k = 0; k < n; k++)
            {
                int e;
                scanf("%d", &e);
                sb[x].insert(e);
                b[x][k] = e;
            }

        vector<int> r(n);
        for (int x = 0; x < m; x++)
            if (*sb[x].lower_bound(a[0][0]) == a[0][0])
                r = b[x];

        for (int y = 0; y < n; y++)
        {
            for (auto e : a[qa[r[y]]])
                printf("%d ", e);
            printf("\n");
        }
    }
    return 0;
}


#include <cstdio>
#include <vector>
#include <utility>

#include <iostream>
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        vector<pair<int, int>> a(n * m+1);
        for (int y = 0; y < n; y++)
            for (int x = 0; x < m; x++)
            {
                int e;
                cin >> e;
                a[e] = {x, y};
            }
        vector<vector<int>> ans(n, vector<int>(m));
        for (int x = 0; x < m; x++)
            for (int y = 0; y < n; y++)
            {
                int e;
                cin >> e;
                // b[e] = {x, y};
                ans[y][a[e].first] = e;
            }
        for (auto &row : ans)
        {
            for (auto f : row)
                printf("%d ", f);
            printf("\n");
        }
    }
    return 0;
}


#include<string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <stack>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <random>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define double long double
#define null nullptr
/*
const int MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
inline void* operator new (size_t n) {
    assert((mpos += n) <= MAX_MEM);
    return (void*)(mem + mpos - n);
}
inline void operator delete (void*) noexcept { }
*/
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("unroll-loops")
*/
//#pragma comment(linker, "/stack:200000000")
//#define _getchar_nolock getchar
//#define _putchar_nolock putchar

#define int long long
using namespace std;
typedef long long ll;

const double pi = 3.1415926535897931159979635l;

int readi() {
	int x = 0;
	char c;
	bool n = false;
	while (((c = _getchar_nolock()) >= '0' && c <= '9') || c == '-') {
		if (c == '-') {
			n = true;
			continue;
		}
		x = (x << 1) + (x << 3) + (c & 15);
	}
	if (n) return -x;
	return x;
}

void outpi(int i) {
	int ln = 0;
	char buf[20];
	if (i < 0) {
		_putchar_nolock('-');
		i = -i;
	}
	if (i == 0) {
		_putchar_nolock('0');
		return;
	}
	while (i) {
		buf[ln] = i % 10 + '0';
		ln++;
		i /= 10;
	}
	for (int j = ln - 1; j >= 0; j--) _putchar_nolock(buf[j]);
}

void outps(string s, bool frst = false) {
	for (int i = 0; i < s.size(); i++) {
		_putchar_nolock(s[i]);
	}
}

bool reads(string& s) {
	s.clear();
	char c;
	while ((c = _getchar_nolock()) != ' ' && c != '\n' && c != EOF && c != ':') {
		s.push_back(c);
	}
	return c == ' ';
}

bool readln(string& s) {
	char c;
	while ((c = _getchar_nolock()) != '\n' && c != EOF) {
		s.push_back(c);
	}
	return c == ' ';
}

typedef long long ll;
#define null nullptr

const bool eq(const vector<int>& v1, const vector<int>& v2) {
	for (int i = 0; i < v1.size(); i++) {
		if (v1[i] != v2[i]) return false;
	}
	return true;
}

signed main() {
	srand(time(NULL));
	int tt = readi();
	while (tt--) {
		int n = readi(), m = readi();
		vector<int> fstlb;
		vector<vector<int>> str(n, vector<int>(m));
		map<int, vector<int>*> sbyval;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				str[i][j] = readi();
				if (j == 0) {
					fstlb.push_back(str[i][j]);
					sbyval[str[i][j]] = &str[i];
				}
			}
		}
		sort(fstlb.begin(), fstlb.end());
		bool otv = false;
		for (int i = 0; i < m; i++) {
			vector<int> crstlb(n);
			for (int j = 0; j < n; j++) {
				crstlb[j] = readi();
			}
			if (otv) continue;
			auto cpy = crstlb;
			sort(cpy.begin(), cpy.end());
			if (eq(cpy, fstlb)) {
				for (auto cri : crstlb) {
					for (auto vv : *sbyval[cri]) {
						outpi(vv);
						_putchar_nolock(' ');
					}
					_putchar_nolock('\n');
				}
				otv = true;
			}
		}
	}
	return 0;
}


#include <iostream>

using namespace std;

int main(){
	int t,n,m,g;
	cin >> t;
	for (int r=0; r < t; r++){
		cin >> n >> m;
		int *A = new int[n*m];
		int *B = new int[n*m];
		int *C = new int[n*m];
		for (int j = 0; j < n*m; j++)
			cin >> A[j];
		for (int j = 0; j < n*m; j++)
			cin >> B[j];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++){
				if (A[i*m + j] == B[0]){
					g = i;
					break;
				}
			}
		for (int j = 0; j < m; j++)
			C[j] = A[g*m + j];
		for (int i = 0; i < m; i++)
			for (int j = 0; j < m; j++){
				if (C[i] == B[j*n]){
					for (int f = 1; f < n; f++)
						C[f*m+i] = B[j*n + f];
				}
			}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++)
				cout << C[i*m + j] << " ";
			cout << endl;
		}
		delete[]A;
		delete[]B;
		delete[]C;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <fstream>
#include <cstdlib>
#include <functional>
#include <climits>
#include <queue>
#include <time.h>
#include <unordered_set>
#include <unordered_map>
#include <stack>

using namespace std;

#pragma GCC optimize("Ofast,unroll-loops,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*
⣿⣿⣿⣿⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣵⣿⣿⣿⠿⡟⣛⣧⣿⣯⣿⣝⡻⢿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⠋⠁⣴⣶⣿⣿⣿⣿⣿⣿⣿⣦⣍⢿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⢷⠄⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣏⢼⣿⣿⣿⣿
⢹⣿⣿⢻⠎⠔⣛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⣿⣿⣿⣿
⢸⣿⣿⠇⡶⠄⣿⣿⠿⠟⡛⠛⠻⣿⡿⠿⠿⣿⣗⢣⣿⣿⣿⣿
⠐⣿⣿⡿⣷⣾⣿⣿⣿⣾⣶⣶⣶⣿⣁⣔⣤⣀⣼⢲⣿⣿⣿⣿
⠄⣿⣿⣿⣿⣾⣟⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⢟⣾⣿⣿⣿⣿
⠄⣟⣿⣿⣿⡷⣿⣿⣿⣿⣿⣮⣽⠛⢻⣽⣿⡇⣾⣿⣿⣿⣿⣿
⠄⢻⣿⣿⣿⡷⠻⢻⡻⣯⣝⢿⣟⣛⣛⣛⠝⢻⣿⣿⣿⣿⣿⣿
⠄⠸⣿⣿⡟⣹⣦⠄⠋⠻⢿⣶⣶⣶⡾⠃⡂⢾⣿⣿⣿⣿⣿⣿
⠄⠄⠟⠋⠄⢻⣿⣧⣲⡀⡀⠄⠉⠱⣠⣾⡇⠄⠉⠛⢿⣿⣿⣿
⠄⠄⠄⠄⠄⠈⣿⣿⣿⣷⣿⣿⢾⣾⣿⣿⣇⠄⠄⠄⠄⠄⠉⠉
⠄⠄⠄⠄⠄⠄⠸⣿⣿⠟⠃⠄⠄⢈⣻⣿⣿⠄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄⠄⢿⣿⣾⣷⡄⠄⢾⣿⣿⣿⡄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄⠄⠸⣿⣿⣿⠃⠄⠈⢿⣿⣿⠄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄[ ПУТИН ПОМОГИ ]⠄⠄⠄⠄⠄⠄
*/

#define ll long long
#define lld long long double
#define magic_define ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(27);
//#define int long long

const int N = 510;
int r[N][N];
set<int> r_s[N];
int c[N][N];
int ans[N][N];

void solve() {
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> r[i][j];
			r_s[i].insert(r[i][j]);
		}
	}

	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> c[i][j];
		}
	}

	for (int x = 0; x < n; ++x) {

		int t0 = c[0][x], t1 = (m == 1 ? c[0][x] : c[1][x]);

		for (int i = 0; i < n; ++i) {
			if (r_s[i].find(t0) != r_s[i].end() && r_s[i].find(t1) != r_s[i].end()) {
				for (int idx = 0; idx < m; ++idx) {
					ans[x][idx] = r[i][idx];
				}
			}
		}

	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << ans[i][j] << " ";
		}
		cout << "\n";
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			ans[i][j] = 0;
		}
	}

	for (int j = 0; j < m; ++j) {
		r_s[j].clear();
	}

}

int32_t main() {
	magic_define;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <fstream>
#include <cstdlib>
#include <functional>
#include <climits>
#include <queue>
#include <time.h>
#include <unordered_set>
#include <unordered_map>
#include <stack>

using namespace std;

#pragma GCC optimize("Ofast,unroll-loops,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*
⣿⣿⣿⣿⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣵⣿⣿⣿⠿⡟⣛⣧⣿⣯⣿⣝⡻⢿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⠋⠁⣴⣶⣿⣿⣿⣿⣿⣿⣿⣦⣍⢿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⢷⠄⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣏⢼⣿⣿⣿⣿
⢹⣿⣿⢻⠎⠔⣛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⣿⣿⣿⣿
⢸⣿⣿⠇⡶⠄⣿⣿⠿⠟⡛⠛⠻⣿⡿⠿⠿⣿⣗⢣⣿⣿⣿⣿
⠐⣿⣿⡿⣷⣾⣿⣿⣿⣾⣶⣶⣶⣿⣁⣔⣤⣀⣼⢲⣿⣿⣿⣿
⠄⣿⣿⣿⣿⣾⣟⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⢟⣾⣿⣿⣿⣿
⠄⣟⣿⣿⣿⡷⣿⣿⣿⣿⣿⣮⣽⠛⢻⣽⣿⡇⣾⣿⣿⣿⣿⣿
⠄⢻⣿⣿⣿⡷⠻⢻⡻⣯⣝⢿⣟⣛⣛⣛⠝⢻⣿⣿⣿⣿⣿⣿
⠄⠸⣿⣿⡟⣹⣦⠄⠋⠻⢿⣶⣶⣶⡾⠃⡂⢾⣿⣿⣿⣿⣿⣿
⠄⠄⠟⠋⠄⢻⣿⣧⣲⡀⡀⠄⠉⠱⣠⣾⡇⠄⠉⠛⢿⣿⣿⣿
⠄⠄⠄⠄⠄⠈⣿⣿⣿⣷⣿⣿⢾⣾⣿⣿⣇⠄⠄⠄⠄⠄⠉⠉
⠄⠄⠄⠄⠄⠄⠸⣿⣿⠟⠃⠄⠄⢈⣻⣿⣿⠄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄⠄⢿⣿⣾⣷⡄⠄⢾⣿⣿⣿⡄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄⠄⠸⣿⣿⣿⠃⠄⠈⢿⣿⣿⠄⠄⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄[ ПУТИН ПОМОГИ ]⠄⠄⠄⠄⠄⠄
*/

#define ll long long
#define lld long long double
#define magic_define ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(27);
//#define int long long

const int N = 510;
int r[N][N];
set<int> r_s[N];
int c[N][N];
vector<int> ans = {};

void solve() {
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> r[i][j];
			r_s[i].insert(r[i][j]);
		}
	}

	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> c[i][j];
		}
	}

	for (int x = 0; x < n; ++x) {

		int fuck_rostislave = c[0][x];

		for (int i = 0; i < n; ++i) {
			if (r_s[i].find(fuck_rostislave) != r_s[i].end()) {
				ans.push_back(i);
				break;
			}
		}

	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << r[ans[i]][j] << " ";
		}
		cout << "\n";
	}

	ans.clear();
	for (int i = 0; i < n; ++i) {
		r_s[i].clear();
	}

}

int32_t main() {
	magic_define;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <set>
#include <fstream>
#include<algorithm>
#include <map>
#include <stack>
#include <queue>

using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); ++i);
typedef long long ll;
typedef vector <ll> vll;
typedef vector <int> vi;
typedef pair <ll, ll> pll;
typedef pair <int, int> ii;
typedef vector <double> vd;
typedef vector <vector <int>> vvi;
const char ENDL = '\n';
void isRight(int n, vi str, vvi ver, vvi &table) {
	for (int i = 0; i < str.size(); i++) {
		bool ok = false;
		for (int j = 0; j < ver.size(); j++) {
			for (int k = 0; k < ver[j].size(); k++) {
				if (ver[j][k] == str[i]) {
					ok = true;
					break;
				}

			}
			if (ok == true) {
				for (int k = 0; k < ver[j].size(); k++) {
					table[k][i] = ver[j][k];
				}
				break;

			}
		}

	}

}
int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, m;
		cin >> n >> m;
		vector <vector <int>> str;
		vector <vector <int>> ver;
		for (int j = 0; j < n; j++) {
			vector <int> now;
			for (int k = 0; k < m; k++) {
				int temp;
				cin >> temp;
				now.push_back(temp);
			}
			str.push_back(now);
		}
		for (int j = 0; j < m; j++) {
			vector <int> now;
			for (int k = 0; k < n; k++) {
				int temp;
				cin >> temp;
				now.push_back(temp);
			}
			ver.push_back(now);
		}
		vector <vector<int>> table;
		for (int j = 0; j < n; j++) {
			vector<int> now(m);
			table.push_back(now);

		}
		isRight(n, str[0], ver, table);
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m; k++) {
				cout << table[j][k] << " ";

			}
			cout << ENDL;
		}
	}

	return 0;
}


#include <iostream>
#include <vector>
#define forn(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> vec(n, vector<int> (m));
        forn(i, n) forn(j, m) cin >> vec[i][j];
        vector<vector<int>> ans(n, vector<int> (m));
        forn(i, m) {
                vector<int> arr(n);
                forn(j, n) {
                    cin >> arr[j];
                }
                int c = 0, count = 0;
                forn(j, n) {
                        forn(q, m) {
                                int s = 0;
                                if (arr[s] == vec[j][q]) {
                                    c = q;
                                    forn(s, n) forn(d, n) if (arr[s] == vec[d][c]) count++;
                                }
                        }
                        if (count == n) break;
                }
                forn(j, n) forn(k, n) if (arr[k] == vec[j][c]) {
                        forn(q, m) ans[k][q] = vec[j][q];
                    }
        }
        forn(i, n) {
             forn(j, m) cout << ans[i][j] << " ";
             cout << endl;
        }
    }
return 0;
}


#include <iostream>

using namespace std;

int a[500][500];
int b[500][500];
int newarr[500][500];
int asum[500]={0};
int bsum[500]={0};

int main(){
    int t;
    int n;
    int m;
    int x;
    int y;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n;
        cin>>m;
        for(int j=0;j<n;j++){
            for(int k=0;k<m;k++){
                cin>>a[j][k];
                asum[j]+=a[j][k];
            }
        }
        for(int j=0;j<m;j++){
            for(int k=0;k<n;k++){
                cin>>b[j][k];
                bsum[j]+=b[j][k];
            }
        }
        for(int j=0;j<n;j++){
            x = a[j][0];
            for(int k=0;k<m;k++){
                for(int q=0;q<n;q++){
                    if(x==b[k][q]){
                        y=q;
                        break;
                    }
                }
            }
            for(int k=0;k<m;k++){
                newarr[y][k]=a[j][k];
            }
        }
        for(int j=0;j<n;j++){
            for(int k=0;k<m;k++){
                cout<<newarr[j][k]<<' ';
            }
            cout<<'\n';
        }
    }
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, m;
		cin >> n >> m;
		vector <vector <int>> a(n);
		for (int j = 0; j < n; j++) {
			a[j].resize(m);
			for (int k = 0; k < m; k++)
				cin >> a[j][k];
		}
		vector <vector <int>> b(m, vector <int>(n));
		int q;
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < n; k++) {
				cin >> b[j][k];
				if (b[j][k] == a[0][0])
					q = j;
			}
		}
		vector <int> ord;
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				if (b[q][j] == a[k][0])
					ord.push_back(k);
			}
		}
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m; k++)
				cout << a[ord[j]][k] << " ";
			cout << endl;
		}
	}
}