#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(vector<int> a, vector<int> b)
{
	return a[0] < b[0];
}
vector<int>* bin_search(int a, vector<vector<int>>& b)
{
	int r = b.size();
	int l = 0;
	while (r - l > 1)
	{
		int m = (r + l) / 2;
		if (b[m][0] <= a)
			l = m;
		else
			r = m;
	}
	if (b[l][0] != a)
		return NULL;
	else
		return &b[l];
}
int main()
{
	int t;
	cin >> t;
	for (int cnt = 0; cnt < t; cnt++)
	{
		int n, m;
		cin >> n >> m;
		vector<vector<int>> row(n, vector<int>(m));
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				cin >> row[i][j];
		vector<vector<int>> column(m, vector<int>(n));
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				cin >> column[i][j];
		sort(row.begin(), row.end(), cmp);
		sort(column.begin(), column.end(), cmp);
		vector<vector<int>> answ(n, vector<int>(m));
		for (auto& i : row)
		{
			vector<int>* c = bin_search(i[0], column);
			if (c != NULL)
			{
				vector<int> x = *c;
				answ[0] = i;
				for (int i = 0; i < n; i++)
					answ[i][0] = x[i];
				break;
			}
		}
		for (int i = 1; i < n; i++)
			answ[i] = *bin_search(answ[i][0], row);
		for (auto& i : answ)
		{
			for (auto& j : i)
				cout << j << ' ';
			cout << endl;
		}
		//cout << endl;
	}
}


#include <iostream>

using namespace std;

const int Max = 505;
const int MAX = Max * Max;

struct point
{
	int x,y;
};

point f[MAX];
int a[Max][Max];

void solve()
{
	int n,m;
	cin >> n >> m;
	for(int i =1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			int e;
			cin >> e;
			f[e].y = j;

		}
	}
	for(int i =1;i<=m;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			int e;
			cin >> e;
			f[e].x = j;
		}
	}
	for(int i = 1;i<=n*m;i++)
	{
		a[f[i].x][f[i].y] = i;
	}
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

int main() {
	int t;
	cin >> t;
	for(int i = 1;i<=t;i++)
	{
		solve();
	}
	return 0;
}


#include <iostream>
using namespace std;
const int Max =1000;
struct point
{
	int b,x,y;
};
int a[Max][Max];
int b[Max][Max];
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];
		}

	}
	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 x = 1;x<=n;x++)
		{
			for(int y = 1;y<=m;y++)
			{
				if(a[x][y] == b[1][i])
				{
					for(int g = 1;g<=m;g++)
					{
						cout << a[x][g] << " ";
					}
					cout << endl;
				}
			}
		}
	}
}
int main() {
	int t;
	cin >> t;
	for(int i = 1;i<=t;i++)
	{
		solve();
	}
	return 0;
}


#include <iostream>

using namespace std;

const int Max = 10000;
const int MM = 600;
struct point
{
	int x,y;
};
point f[Max];
int a[MM][MM];
void solve()
{
	int n,m;
	cin >> n >> m;
	for(int i =1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			int e;
			cin >> e;
			f[e].y = j;

		}
	}
	for(int i =1;i<=m;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			int e;
			cin >> e;
			f[e].x = j;
		}
	}
	for(int i = 1;i<=n*m;i++)
	{
		a[f[i].x][f[i].y] = i;
	}
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

int main() {
	int t;
	cin >> t;
	for(int i = 1;i<=t;i++)
	{
		solve();
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
typedef long long ll;
#pragma GCC optimize("Ofast,inline,unroll-loops,no-stack-protector")
using namespace std;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	for (int _ = 0; _ < t; _++) {
		int n, m;
		cin >> n >> m;
		vector<vector<int>> ans(n, vector<int>(m));
		set<pair<int,int>> s;
		vector<vector<int>> rows(n, vector<int> (m));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cin >> rows[i][j];
			s.insert({ rows[i][0], i });
		}
		vector<int> col;
		for (int i = 0; i < m; i++) {
			vector<int> rc(n);
			for (int j = 0; j < n; j++) {
				cin >> rc[j];
			}
			auto it = s.lower_bound({ rc[0], 0 });
			if (it != s.end() && it->first == rc[0]) {
				col = rc;
			}
		}
		for (int i = 0; i < n; i++) {
			int ind = s.lower_bound({ col[i], 0 })->second;
			for (int j = 0; j < m; j++)
				cout << rows[ind][j] << " ";
			cout << endl;
		}
	}
}


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

using namespace std;

////////////////////////////////////////////////////////////

int input() {
	int t;
	cin >> t;
	return t;
}

vector<int> input(int n) {
	vector<int> v(n);
	for (int i = 0; i < n; ++i)
		cin >> v[i];
	return v;
}

void print(vector<int>& v) {
	for (int u : v)
		cout << u << ' ';
	cout << endl;
}

////////////////////////////////////////////////////////////

void solve() {
	int n = input(), m = input();

	vector<vector<int>> rows(n, vector<int>(m));
	vector<vector<int>> cols(m, vector<int>(n));

	vector<vector<int>> res(n, vector<int>(m, 0));

	for (int i = 0; i < n; ++i) rows[i] = input(m);
	for (int i = 0; i < m; ++i) cols[i] = input(n);

	for (int row = 0; row < n; ++row)
		for (int col = 0; col < m; ++col)
			if (rows[row][0] == cols[col][0]) {
				res[0] = rows[row];
				row = n;
				col = m;
			}

	for (int col = 0; col < m; ++col)
		for (int col2 = 0; col2 < m; ++col2)
			if (res[0][col] == cols[col2][0])
				for (int i = 1; i < n; i++)
					res[i][col] = cols[col2][i];

	for (int i = 0; i < n; ++i)
		print(res[i]);
}

int main() {
	int t = input();

	for (int i = 0; i < t; ++i)
		solve();

	return 0;
}


#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int find1(vector<int> k,int k1){
    for(int i=0;i<k.size();i++){
        if(k[i]==k1) return i;
    }
    return -1;
}

int main(){
    int a,b,c,v,t;
    cin>>a;
    for(int i=0;i<a;++i){
        cin>>b>>c;
        vector<vector<int> > d(b,vector<int> (c));
        vector<vector<int> > d1(b);
        vector<int> k(b);
        for(int j=0;j<b;j++){
                for(int j1=0;j1<c;j1++){
            cin>>d[j][j1];}
            k[j]=d[j][0];
        }
        for(int p=0;p<c;p++){
                vector<int> k1(b);
            for(int p1=0;p1<b;p1++){
                cin>>k1[p1];
            }
        t=find1(k,k1[0]);
                if (t!=-1){
                    for(int y=0;y<b;y++){
                        t=find1(k,k1[y]);
                        d1[y]=d[t];
                    }
                }
        }
        for(int x=0;x<b;x++){
                if(x!=0) cout<<"\n";
            for(int y=0;y<c;y++){
                    cout<<d1[x][y]<<" ";};
        }
        cout<<"\n";
    }
}


#include <iostream>
#include <vector>
using namespace std;

void f()
{
    long cur;
    int n, m;
    bool TrueOrFalse;
    cin >> n >> m;
    vector<long> s;
    vector<vector<long>> raw;
    for (int i = 0; i < n; i++)
    {
        vector<long> r;
        for (int j = 0; j < m; j++)
        {
            cin >> cur;
            r.push_back(cur);
        }
        raw.push_back(r);
    }
    cur = raw[0][0];
    for (int i = 0; i < m; i++)
    {
        TrueOrFalse = false;
        vector<long> r;
        for (int j = 0; j < n; j++)
        {
            long curr;  cin >> curr;
            r.push_back(curr);
            if (curr == cur)
            {
                TrueOrFalse = true;
            }
        }
        if (TrueOrFalse)
        {
            for (int k = 0; k < n; k++)
            {
                s.push_back(r[k]);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (raw[j][0] == s[i])
            {
                for (int k = 0; k < m; k++)
                {
                    cout << raw[j][k] << " ";
                }
                cout << endl;
            }
        }
    }
}

int main()
{
    long t;
    cin >> t;
    for (long l = 0; l < t; l++)
    {
        f();
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <string.h>
#include <set>

using namespace std;

using ll = long long;

#define mp make_pair
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)

#define a first
#define b second

int main() {
	vector<vector<ll>> abc(500);
	vector<vector<ll>> def(500);
	vector<vector<ll>> answer(500);
	ll a, b, c, y = 0;
	cin >> a;
	for (ll i = 0; i < a; i++){
		ll n, m;
		cin >> n >> m;
		for (ll j = 0; j < n; j++){
			for (ll l = 0; l < m; l++){
				ll T;
				cin >> T;
				abc[j].push_back(T);
			}
		}
		for (ll j = 0; j < m; j++){
			for (ll l = 0; l < n; l++){
				ll T;
				cin >> T;
				def[j].push_back(T);
			}
		}
		/*bool flag = false;
		for (ll j = 0; j < n; j++){
			for (ll l = 0; l < m; l++){
				if (abc[l][0] == def[j][0]) {
					for (ll q = 0; q < n; q++){
						cout << abc[l][q] << " " << endl;
					}
					for (ll q = 1; q < n; q++){
						for (ll w = 0; w < m; w++){
							if (def[j][q] == abc[w][0]) {
								for (ll y = 0; y < n; y++){
									cout << abc[l][q] << " ";
								}
								cout << endl;
							}
						}
					}
					flag = true;
					break;
				}
			}
			if (flag) break;
		}*/
		for (ll j = 0; j < m; j++){
			bool flag2 = false;
			for (ll l = 0; l < n; l++){
				bool flag = false;
				if (def[j][y] == abc[l][0]) {
					for (ll q = 0; q < m; q++){
						cout << abc[l][q] << " ";
						flag = true;
					}
				}
				if (flag) {
					cout << endl;
					y++;
					l=-1;
					flag2 = true;
				}
			}
			y = 0;
			if (flag2) break;

		}
		for (ll j = 0; j < n; j++)
			abc[j].clear();
		for (ll j = 0; j < m; j++)
			def[j].clear();
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <string.h>
#include <set>

using namespace std;

using ll = long long;

#define mp make_pair
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)

#define a first
#define b second

int main() {
	vector<vector<ll>> abc(500);
	vector<vector<ll>> def(500);
	vector<vector<ll>> answer(500);
	ll a, b, c, y = 0;
	cin >> a;
	for (ll i = 0; i < a; i++){
		ll n, m;
		cin >> n >> m;
		for (ll j = 0; j < n; j++){
			for (ll l = 0; l < m; l++){
				ll T;
				cin >> T;
				abc[j].push_back(T);
			}
		}
		for (ll j = 0; j < m; j++){
			for (ll l = 0; l < n; l++){
				ll T;
				cin >> T;
				def[j].push_back(T);
			}
		}
		for (ll j = 0; j < m; j++){
			bool flag2 = false;
			for (ll l = 0; l < n; l++){
				bool flag = false;
				if (def[j][y] == abc[l][0]) {
					for (ll q = 0; q < m; q++){
						cout << abc[l][q] << " ";
						flag = true;
					}
				}
				if (flag) {
					cout << endl;
					y++;
					l=-1;
					flag2 = true;
				}
			}
			if (flag2) break;
		}
		y = 0;
		for (ll j = 0; j < n; j++)
			abc[j].clear();
		for (ll j = 0; j < m; j++)
			def[j].clear();
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <string.h>
#include <set>

using namespace std;

using ll = long long;

#define mp make_pair
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)

#define a first
#define b second

int main() {
	vector<vector<ll>> abc(500);
	vector<vector<ll>> def(500);
	vector<vector<ll>> answer(500);
	ll a, b, c, y = 0;
	cin >> a;
	for (ll i = 0; i < a; i++){
		ll n, m;
		cin >> n >> m;
		for (ll j = 0; j < n; j++){
			for (ll l = 0; l < m; l++){
				ll T;
				cin >> T;
				abc[j].push_back(T);
			}
		}
		for (ll j = 0; j < m; j++){
			for (ll l = 0; l < n; l++){
				ll T;
				cin >> T;
				def[j].push_back(T);
			}
		}
		bool flag = false;
		for (ll j = 0; j < m; j++){
			for (ll l = 0; l < n; l++){
				if (abc[l][0] == def[j][0]) {
					for (ll q = 0; q < m; q++){
						cout << abc[l][q] << " ";
					}
					cout << endl;
					for (ll q = 1; q < n; q++){
						for (ll w = 0; w < n; w++){
							if (def[j][q] == abc[w][0]) {
								for (ll y = 0; y < m; y++){
									cout << abc[w][y] << " ";
								}
								cout << endl;
							}
						}
					}
					flag = true;
					break;
				}
			}
			if (flag) break;
		}
		/*for (ll j = 0; j < m; j++){
			bool flag2 = false;
			for (ll l = 0; l < n; l++){
				bool flag = false;
				if (def[j][y] == abc[l][0]) {
					for (ll q = 0; q < m; q++){
						cout << abc[l][q] << " ";
						flag = true;
					}
				}
				if (flag) {
					cout << endl;
					y++;
					l=-1;
					flag2 = true;
				}
			}
			y = 0;
			if (flag2) break;

		}*/
		for (ll j = 0; j < n; j++)
			abc[j].clear();
		for (ll j = 0; j < m; j++)
			def[j].clear();
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <string.h>
#include <set>

using namespace std;

using ll = long long;

#define mp make_pair
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)

#define a first
#define b second

int main() {
	vector<vector<ll>> abc(500);
	vector<vector<ll>> def(500);
	vector<vector<ll>> answer(500);
	ll a, b, c, y = 0;
	cin >> a;
	for (ll i = 0; i < a; i++){
		ll n, m;
		cin >> n >> m;
		for (ll j = 0; j < n; j++){
			for (ll l = 0; l < m; l++){
				ll T;
				cin >> T;
				abc[j].push_back(T);
			}
		}
		for (ll j = 0; j < m; j++){
			for (ll l = 0; l < n; l++){
				ll T;
				cin >> T;
				def[j].push_back(T);
			}
		}
		for (ll j = 0; j < m; j++){
			for (ll l = 0; l < n; l++){
				bool flag = false;
				if (def[j][y] == abc[l][0]) {
					for (ll q = 0; q < m; q++){
						cout << abc[l][q] << " ";
						flag = true;
					}
				}
				if (flag) {
					cout << endl;
					y++;
					l=-1;
				}
			}
		}
		y = 0;
		for (ll j = 0; j < n; j++)
			abc[j].clear();
		for (ll j = 0; j < m; j++)
			def[j].clear();
	}
}


#include <iostream>
#include <string>
#include <vector>
//#include <stack>
#include <algorithm>

using namespace std;

int find(vector<vector<int>>&a, int v) {
	for (int i = 0; i < a.size(); i++) {
		if (find(a.at(i).begin(), a.at(i).end(), v) != a.at(i).end()) {
			return i;
		}
	}

	return -1;
}

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

	while (N--) {
		vector<vector<int>> s,c;//mn, mn
		int m, n;//m-rows, n -colums, n-y,m-x
		cin >> n >> m;

		for (int i = 0; i < n; i++)
		{
			s.push_back(vector<int>(m));
			for (int j = 0; j < m; j++)
			{
				cin >> s.at(i).at(j);
			}
		}
		for (int i = 0; i < m; i++)
		{
			c.push_back(vector<int>(n));
			for (int j = 0; j < n; j++)
			{
				cin >> c.at(i).at(j);
			}
		}

		vector<vector<int>> mat;

		for (int i = 0, t, f; i < n; i++)
		{
			t = c[0][i];

			f = find(s, t);

			mat.push_back(s[f]);
		}

		for (auto&a : mat) {
			for (auto b : a)
				cout << b << ' ';
			cout << endl;
		}
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main () {
    int t, n, m, x;
    cin >> t;
    for (int y = 0; y < t; y++) {
        cin >> n >> m;
        queue <int> qu;
        vector <vector <int> > a (n, vector <int> (m + 1, 0));
        vector <vector <int> > b (n, vector <int> (m + 1, 0));
        for (int i = 0; i < n; i++) {
            qu.push (i);
            for (int j = 0 ; j <m; j++) {
                cin >> a[i][j];
                a[i][m] += a[i][j];
            }
        }
        for (int i=0; i < m; i++) {
            for (int j =0; j < n; j++) {
                cin >> b[j][i];
                b[j][m] += b[j][i];
            }
        }

        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                if (b[0][0] == a[i][j]) {
                    for (int ii=0; ii<n; ii++){
                        int now = qu.front();
                        qu.pop();
                        while (b[ii][0] != a[now][j] ) {
                            qu.push(now);
                            now = qu.front();
                            qu.pop();
                        }
                        if (b[ii][0] == a[now][j]) {
                            for (int uu = 0; uu < m; uu++) cout << a[now][uu] << " ";
                            cout << endl;
                        }
                    }
                }
            }
        }
    }
    return 0;
}


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

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

using namespace std;

const int INF = 10000000;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    //freopen("minonpath.in", "r", stdin);
    //freopen("minonpath.out", "w", stdout);
    int t;
    cin >> t;
    for (int step = 0; step < t; step++) {
        int n, m;
        cin >> m >> n;
        vector<vector<int>> rows(m, vector<int>(n)), cols(n, vector<int>(m));
        for (auto &i : rows)
            for (auto &j : i)
                cin >> j;
        for (auto &i : cols)
            for (auto &j : i)
                cin >> j;
        int pos_col_zero = 0;
        for (int  i = 0; i < n; i++) {
            for (auto &j : cols[i])
                if (j == rows[0][0])
                    pos_col_zero = i;
        }
        vector<int> ans(m, 0);
        map<int, int> where;
        for (int i = 0; i < m; i++) {
            where[cols[pos_col_zero][i]] = i;
        }
        for (int i = 0; i < m; i++) {
            int pos = where[rows[i][0]];
            ans[pos] = i;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cout << rows[ans[i]][j] << " ";
            }
            cout << "\n";
        }

    }
}

/*
1
4 4
1 2 3 4
13 14 15 16
5 6 7 8
9 10 11 12
2 6 10 14
3 7 11 15
4 8 12 16
1 5 9 13
 */


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

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

using namespace std;

const int INF = 10000000;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    //freopen("minonpath.in", "r", stdin);
    //freopen("minonpath.out", "w", stdout);
    int t;
    cin >> t;
    for (int step = 0; step < t; step++) {
        int n, m;
        cin >> m >> n;
        vector<vector<int>> rows(m, vector<int>(n)), cols(n, vector<int>(m));
        for (auto &i : rows)
            for (auto &j : i)
                cin >> j;
        for (auto &i : cols)
            for (auto &j : i)
                cin >> j;
        vector<int> ans(m, -1);
        vector<pair<int, int>> where(m);
        for (int i = 0; i < m; i++) {
            where[i] = {cols[0][i], i};
        }
        sort(where.begin(), where.end());
        for (int i = 0; i < m; i++) {
            int pos = lower_bound(where.begin(), where.end(), make_pair(rows[i][0], -INF)) - where.begin();
            ans[where[pos].second] = i;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cout << rows[ans[i]][j] << " ";
            }
            cout << "\n";
        }

    }
}


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

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

using namespace std;

const int INF = 10000000;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    //freopen("minonpath.in", "r", stdin);
    //freopen("minonpath.out", "w", stdout);
    int t;
    cin >> t;
    for (int step = 0; step < t; step++) {
        int n, m;
        cin >> m >> n;
        vector<vector<int>> rows(m, vector<int>(n)), cols(n, vector<int>(m));
        for (auto &i : rows)
            for (auto &j : i)
                cin >> j;
        for (auto &i : cols)
            for (auto &j : i)
                cin >> j;
        vector<int> ans(m, 0);
        vector<pair<int, int>> where(m);
        for (int i = 0; i < m; i++) {
            where[i] = {cols[0][i], i};
        }
        sort(where.begin(), where.end());
        for (int i = 0; i < m; i++) {
            int pos = lower_bound(where.begin(), where.end(), make_pair(rows[i][0], -INF)) - where.begin();
            ans[where[pos].second] = i;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cout << rows[ans[i]][j] << " ";
            }
            cout << "\n";
        }

    }
}


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

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

using namespace std;

const int INF = 10000000;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    //freopen("minonpath.in", "r", stdin);
    //freopen("minonpath.out", "w", stdout);
    int t;
    cin >> t;
    for (int step = 0; step < t; step++) {
        int n, m;
        cin >> m >> n;
        vector<vector<int>> rows(m, vector<int>(n)), cols(n, vector<int>(m));
        for (auto &i : rows)
            for (auto &j : i)
                cin >> j;
        for (auto &i : cols)
            for (auto &j : i)
                cin >> j;
        vector<int> ans(m, 0);
        map<int, int> where;
        for (int i = 0; i < m; i++) {
            where[cols[0][i]] = i;
        }
        for (int i = 0; i < m; i++) {
            int pos = where[rows[i][0]];
            ans[pos] = i;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cout << rows[ans[i]][j] << " ";
            }
            cout << "\n";
        }

    }
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int t, n, m, c;
    vector<vector<int>> a(500, vector<int>(500));
    vector<vector<int>> b(500, vector<int>(500));
    vector<vector<int>> ans(500, vector<int>(500));
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> n >> m;
        for (int j = 0; j < n; j++) {
            for (int o = 0; o < m; o++) {
                cin >> a[j][o];

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

        for (int j = 0; j < n; j++) {
            c = b[0][j];
            for (int o1 = 0; o1 < n; o1++) {
                for (int o2 = 0; o2 < m; o2++) {
                    if (c == a[o1][o2]) {
                        ans[j] = a[o1];
                    }
                }
            }
        }
        for (int j = 0; j < n; j++) {
            for (int o = 0; o < m; o++) {
                cout << ans[j][o] << " ";
            }
            cout << endl;
        }

    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int n, m;
		cin >> n >> m;
		vector<int> ans;
		vector<vector<int>> t(n, vector<int>(m));
		vector<int> in(n * m + 1);
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m; k++) {
				cin >> t[j][k];
				in[t[j][k]] = j;
			}
		}
		int c;
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < n; k++) {
				cin >> c;
				if (j == 0)ans.push_back(in[c]);
			}
		}
		for (auto el : ans) {
			for (int j = 0; j < m; j++) cout << t[el][j] << " ";
			cout << endl;
		}
	}

}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

using namespace std;

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

    for (int q = 0; q < t; ++q)
    {
        int n, m;
        cin >> n >> m;

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

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

        vector<int> vec;
        int a = graph2[0][0];
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (graph[i][j] == a)
                {
                    for (int k = 0; k < m; ++k)
                        vec.push_back(graph[i][k]);
                    break;
                }
            }
        }

        /*
        for (int i = 0; i < vec.size(); ++i)
            cout << vec[i] << ' ';
        */

        vector<vector<int>> finalAnswer(m);
        for (int k = 0; k < m; ++k)
        {
            for (int i = 0; i < m; ++i)
            {
                if (vec[k] == graph2[i][0])
                {
                    finalAnswer[k].resize(n);
                    for (int j = 0; j < n; ++j)
                        finalAnswer[k][j] = graph2[i][j];
                    break;
                }
            }
        }

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

        cout << "\n";
    }

    return 0;
}


// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#define LL long long

using namespace std;

int const SIZE = 505;

int first[SIZE][SIZE];
int second[SIZE][SIZE];

int main()
{
	cin.sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int T;
	cin >> T;

	for (int t = 0; t < T; t++)
	{
		bool visited[SIZE]{};
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cin >> first[i][j];
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++)
				cin >> second[i][j];
		}

		for (int i = 0; i < n; i++) {
			if (visited[i]) continue;
			int x = second[0][i];
			for (int ii = 0; ii < n; ii++) {
				for (int j = 0; j < m; j++) {
					if (first[ii][j] == x) {
						for (int jj = 0; jj < m; jj++) cout << first[ii][jj] << " ";
						cout << "\n";
						visited[i] = true;
						i = -1;
						break;
					}
				}
				if (i == -1) break;
			}
		}
	}

	return 0;
}


// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#define LL long long

using namespace std;

int const SIZE = 505;

int first[SIZE][SIZE];
int second[SIZE][SIZE];

int main()
{
	cin.sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int T;
	cin >> T;

	for (int t = 0; t < T; t++)
	{
		bool visited[SIZE]{};
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cin >> first[i][j];
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++)
				cin >> second[i][j];
		}

		for (int i = 0; i < n; i++) {
			if (visited[i]) continue;
			int x = second[0][i];
			for (int j = 0; j < n; j++) {
				if (first[j][0] == x) {
					for (int a = 0; a < m; a++)
						cout << first[j][a] << " ";
					cout << "\n";
					visited[i] = true;
					i = 0;
					break;
				}
			}
		}
	}

	return 0;
}


// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#define LL long long

using namespace std;

int const SIZE = 505;

int first[SIZE][SIZE];
int second[SIZE][SIZE];

int main()
{
	cin.sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int T;
	cin >> T;

	for (int t = 0; t < T; t++)
	{
		bool visited[SIZE]{};
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cin >> first[i][j];
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++)
				cin >> second[i][j];
		}

		for (int i = 0; i < m; i++) {
			if (visited[i]) continue;
			int x = second[i][0];
			for (int ii = 0; ii < n; ii++) {
				for (int j = 0; j < m; j++) {
					if (first[ii][j] == x) {
						for (int jj = 0; jj < m; jj++) cout << first[ii][jj] << " ";
						cout << "\n";
						visited[i] = true;
						i = -1;
						break;
					}
				}
				if (i == -1) break;
			}
		}
	}

	return 0;
}


// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#define LL long long

using namespace std;

int const SIZE = 505;

int first[SIZE][SIZE];
int second[SIZE][SIZE];
bool visited[SIZE]{};

int main()
{
	cin.sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int T;
	cin >> T;

	for (int t = 0; t < T; t++)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				cin >> first[i][j];
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++)
				cin >> second[i][j];
		}

		for (int i = 0; i < n; i++) {
			if (visited[i]) continue;
			int x = second[0][i];
			for (int j = 0; j < n; j++) {
				if (first[j][0] == x) {
					for (int a = 0; a < m; a++)
						cout << first[j][a] << " ";
					cout << "\n";
					visited[i] = true;
					i = 0;
					break;
				}
			}
		}
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <random>
#include <chrono>
#include <cassert>

using namespace std;

int main() {
    ios::sync_with_stdio(false);

    int t;
    cin >> t;

    for (int i = 0; i < t; i++) {
        int n, m;
        cin >> n >> m;

        vector<vector<int>> arr1(n, vector<int>(m));
        vector<vector<int>> arr2(m, vector<int>(n));

        vector<set<int>> s (n);

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

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

        vector <vector<int>> ans(n, vector<int>(m));

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (s[j].count(arr2[0][i]) != 0) {
                    for (int k = 0; k < m; k++) {
                        ans[i][k] = arr1[j][k];
                    }
                    break;
                }
            }
        }

        for (auto i : ans) {
            for (auto j : i) {
                cout << j << " ";
            }
            cout << "\n";
        }
    }

    return 0;
}


/*#define _USE_MATH_DEFINES
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")*/

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <functional>
#include <set>
#include <map>
#include <random>
#include <cstdlib>
#include <memory>
#include <assert.h>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <stack>

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
bool Debug = true;

#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define forn(i, n) for (ll i = 0; i < (n); ++i)
#define pi 3.141592653589793238462643383279
#define vll vector <ll>
#define ve vector
#define de deque
#define sll set <ll>
#define msll multiset <ll>
#define dll deque <ll>
#define pll pair <ll, ll>
#define print(h) for (auto ijkl : h) cout << ijkl << " "; cout << "\n"
#define prints(h, s) for (ll ijkl = 0; ijkl < h.size(); ++ijkl) cout << h[ijkl] << s; cout << "\n"
#define printvpll(h) for (auto ijkl : h) cerr << ijkl.ft << ":" << ijkl.sd << " "; cerr << "\n";
#define scan(h, n) for (ll ijkl = 0; ijkl < n; ++ijkl) cin >> h[ijkl]
#define INF 1e18
#define pow2(n) (n)    *    (n)
#define pow3(n) (n) * (n) * (n)
#define all(x) (x).begin(), (x).end()
#define MOD ll(1e9 + 7)
#define MOD2 ll(998244353)
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define elif else if
#define ft first
#define sd second
#define look1(x)          cerr << #x << ": " << x << endl;
#define look2(x, y)       cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define look3(x, y, z)    cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define look4(a, b, c, d) cerr << #a << ":" << a << " | " << #b << ":" << b << " | " << #c << ":" << c << " | " << #d << ":" << d << "\n";

/*ll f() {
    int a = 0;
    char c = ' ';
    bool flag = false;
    while (!isdigit(c))
    {
        c = _getchar_nolock();
        if (c == '-')
            flag = true;
    }
    while (isdigit(c)) {
        a = a * 10 + (c - '0');
        c = _getchar_nolock();
    }
    if (flag)
        return -a;
    return a;
}*/

ll maxn(ve <ll>& h) {
    ll ans = -INF;
    for (int ijkl = 0; ijkl < h.size(); ++ijkl) {
        ans = max(ans, (ll)h[ijkl]);
    }
    return ans;
}

ll minn(ve <ll>& h) {
    ll ans = INF;
    for (int ijkl = 0; ijkl < h.size(); ++ijkl) {
        ans = min(ans, (ll)h[ijkl]);
    }
    return ans;
}

ll sumn(vll& h) {
    ll ans = 0;
    for (int ijkl = 0; ijkl < h.size(); ++ijkl) {
        ans += h[ijkl];
    }
    return ans;
}

ll pown(ll n, ll cnt) {
    //look1(cnt);
    if (cnt == 0) {
        return 1;
    }
    if (cnt == 1) {
        return n;
    }
    if (cnt % 2 == 0) {
        ll a = pown(n, cnt / 2);
        return (a * a) % MOD2;
    }
    else {
        ll a = pown(n, (cnt - 1) / 2);
        return (a * a) % MOD2 * n % MOD2;
    }
}

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);
}

/*ld dist(ld x1, ld y1, ld x2, ld y2) {
    return sqrt(pow2(x1 - x2) + pow2(y1 - y2));
}*/

struct Point {
    int x, y;
    Point() : x(0), y(0) {}
    Point(int _x, int _y) : x(_x), y(_y) {}
    Point(Point a, Point b) : x(b.x - a.x), y(b.y - a.y) {}
};

string alph = "abcdefghijklmnopqrstuvwxyz";

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        ll n, m;
        cin >> n >> m;
        ve <vll> a(n, vll(m)), b(m, vll(n)), ans(n, vll(m));
        for (int i = 0; i < n; ++i) {
            scan(a[i], m);
        }
        for (int i = 0; i < m; ++i) {
            scan(b[i], n);
        }
        vll to_ans;
        sll to_use;
        map <ll, ll> ind;
        for (int i = 0; i < m; ++i) {
            to_use.insert(b[i][0]);
            ind[b[i][0]] = i;
        }
        for (int i = 0; i < n; ++i) {
            sll to_check;
            for (int j = 0; j < m; ++j) {
                to_check.insert(a[i][j]);
            }
            if (to_check == to_use) {
                for (int j = 0; j < m; ++j) {
                    for (int k = 0; k < n; ++k) {
                        ans[k][j] = b[ind[a[i][j]]][k];
                    }
                }
                break;
            }
        }
        for (int i = 0; i < n; ++i) {
            print(ans[i]);
        }
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iterator>
using namespace std;
int main()
{
	int t, h;
	cin >> t;
	for (int k = 0; k < t; k++) {
		int n, m;
		cin >> n;
		cin >> m;
		vector<int> ns;

		for (int i = 0; i < n; i++) {
			ns.push_back(0);
		}
		vector<vector<int>>sps;
		vector<int>stolb;

		for (int i = 0; i < n; i++) {
			vector<int>n1;
			n1.reserve(m);
			for (int j = 0; j < m; j++) {
				cin >> h;
				n1.push_back(h);
			}
			sps.push_back(n1);
		}
		for (int i = 0; i < n; i++) {
			cin >> h;
			stolb.push_back(h);

		}
		for (int i = 0; i < (m*n - n); i++) {
			cin >> h;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
			if (find(sps[j].begin(), sps[j].end(), stolb[i] ) != sps[j].end()){
				ns[j] = i;
			}
			}

		}
		for (int i = 0; i < n; i++) {
			int k;
			k = find(ns.begin(), ns.end(), i) - ns.begin();

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

	}

}


#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <unordered_map>
#include <string>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for (int tmp = 0; tmp < t; ++tmp)
	{
		int n, m;
		cin >> n >> m;
		vector<vector<int>> table(n, vector<int>(m));
		vector<int> numStr(n*m + 1);
		vector<int> order(n);
		auto strs = table;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
			{
				cin >> strs[i][j];
				numStr[strs[i][j]] = i;
			}

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

		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
				table[order[i]][j] = strs[i][j];

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


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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

int main() {
	ll t;
	cin >> t;
	while (t--) {
		ll n, m;
		cin >> n >> m;
		vector<vector<int> > a(n);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int h;
				cin >> h;
				a[i].push_back(h);
			}
		}

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

		//resolve
		for (int k = 0; k < n; k++) { //cmp stls
			vector<int> cmp2 = b[k];

			for (int i = 0; i < n; i++) { //iterate a
				bool flag = true;
				for (int j = 0; j < m; j++) {
					if (find(a[i].begin(), a[i].end(), cmp2[j]) == a[i].end()) {
						flag = false;
						break;
					}
				}
				if (flag) {
					for (int j = 0; j < m; j++) {
						cout << a[i][j] << " ";
					}
					cout << endl;
					break;
				}
			}

		}
	}
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	int T;
	cin >> T;
	for (int c = 0; c != T; ++c) {

		int n, m;
		cin >> n >> m;
		vector<vector<int>> r(n, vector<int>(m));
		vector<int> need_col(n);

		for (int i = 0; i != n; ++i)
			for (int j = 0; j != m; ++j)
				cin >> r[i][j];

		int f = r[0][0];

		bool found = false;
		for (int i = 0; i != m; ++i) {
			for (int j = 0; j != n; ++j) {
				int x;
				cin >> x;
				if (!found) {
					need_col[j] = x;
					if (x == f) found = true;
				}
			}
		}

		vector<vector<int>> o(n, vector<int>(m));
		for (int i = 0; i != n; ++i) {
			for (int j = 0; j != n; ++j) {
				if (need_col[j] == r[i][0]) {
					o[j] = r[i];
				}
			}
		}

		for (int i = 0; i != n; ++i) {
			for (int j = 0; j != m; ++j)
				cout << o[i][j] << ' ';
			cout << '\n';
		}

	}
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	int T;
	cin >> T;
	for (int c = 0; c != T; ++c) {

		int n, m;
		cin >> n >> m;
		vector<vector<int>> r(n, vector<int>(m));
		vector<int> need_col(n);

		for (int i = 0; i != n; ++i)
			for (int j = 0; j != m; ++j)
				cin >> r[i][j];

		int f = r[0][0];

		bool found = false;
		bool fullcol = false;
		for (int i = 0; i != m; ++i) {
			for (int j = 0; j != n; ++j) {
				int x;
				cin >> x;
				if (!fullcol) {
					need_col[j] = x;
					if (x == f) found = true;
				}
			}
			if (found) fullcol = true;
		}

		vector<vector<int>> o(n, vector<int>(m));
		for (int i = 0; i != n; ++i) {
			for (int j = 0; j != n; ++j) {
				if (need_col[j] == r[i][0]) {
					o[j] = r[i];
				}
			}
		}

		for (int i = 0; i != n; ++i) {
			for (int j = 0; j != m; ++j)
				cout << o[i][j] << ' ';
			cout << '\n';
		}

	}
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m, k = -1, x, y;
		set<int>C;
		cin >> n >> m;
		vector<vector<int>>a(n*m + 1, vector<int>(m + 1)), b(m + 1, vector<int>(n + 1));
		for (int i = 0; i < n; i++)
			for (int u = 0; u < m; u++)
			{
				cin >> x;
				if (!u)
					y = x, C.insert(y);
				a[y][u] = x;
			}
		for (int i = 0; i < m; i++)
		{
			set<int>D;
			for (int u = 0; u < n; u++)
				cin >> b[i][u], D.insert(b[i][u]);
			if (k == -1 && C == D)
				k = i;
		}
		for (int i = 0; i < n; i++)
		{
			for (int u = 0; u < m; u++)
				cout << a[b[k][i]][u] << ' ';
			cout << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;

int a[501][501], b[501][501];

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m, k = -1, x, y;
		set<int>C;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			for (int u = 0; u < m; u++)
			{
				cin >> x;
				if (!u)
					y = x, C.insert(y);
				a[y][u] = x;
			}
		for (int i = 0; i < m; i++)
		{
			set<int>D;
			for (int u = 0; u < n; u++)
				cin >> b[i][u], D.insert(b[i][u]);
			if (k == -1 && C == D)
				k = i;
		}
		for (int i = 0; i < n; i++)
		{
			for (int u = 0; u < m; u++)
				cout << a[b[k][i]][u] << ' ';
			cout << endl;
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <string.h>

using namespace std;
vector<vector<int> > m1;
map <int, int> d;
vector<vector<int> > m2;
int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {

		int n, m;
		cin >> n >> m;
		m1.resize(n);
		m2.resize(n);
		d.clear();
		for (int j = 0; j < n; ++j) {
			for (int j1 = 0; j1 < m; ++j1) {
				int dop;
				cin >> dop;
				m1[j].push_back(dop);
			}
		}
		for (int j = 0; j < m; ++j) {
			for (int j1 = 0; j1 < n; ++j1) {
				int dop;
				cin >> dop;
				d[dop] = j1;
			}
		}
		for (int j = 0; j < n; ++j) {
			int dop = d[m1[j][0]];
			m2[dop] = m1[j];
		}
		for (int j = 0; j < n; ++j) {
			for (int j1 = 0; j1 < m2[j].size(); ++j1) {
				if (j1 < m - 1) {
					cout << m2[j][j1] << ' ';
				}
				else {
					cout << m2[j][j1] << endl;
				}
			}
		}
		m1.clear();
		m2.clear();
		d.clear();
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int t, n, m;

	cin >> t;

	for (int i = 0; i < t; i++)
	{
		cin >> n >> m;
		vector < vector <int> > ans(n);
		vector < vector < int > > s(n);
		for (int i = 0; i < n; i++)
		{
			ans[i].resize(m);
			s[i].resize(m);
			for (int j = 0; j < m; j++)
			{
				cin >> s[i][j];
			}
		}
		vector < vector < int > > r(m);
		for (int i = 0; i < m; i++)
		{
			r[i].resize(n);
			for (int j = 0; j < n; j++)
			{
				cin >> r[i][j];
			}
		}
		for (int i = 0; i < n; i++)
		{
			int res = -1;
			for (int j = 0; j < m; j++)
			{
				for (int k = 0; k < n; k++)
				{
					if (s[i][j] == r[0][k])
					{
						res = k;
						break;
					}
				}
				if (res != -1)
				{
					break;
				}
			}
			for (int j = 0; j < m; j++)
			{
				ans[res][j] = s[i][j];
			}
		}
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				cout << ans[i][j] << " ";
			}
			cout << endl;
		}
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include<fstream>

using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		vector <vector <int>> a(n);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int f;
				cin >> f;
				a[i].push_back(f);
			}
		}
		int min_first = 1e9;
		for (int i = 0; i < n; i++) {
			min_first = min(min_first, a[i][0]);
		}

		vector <vector <int>> b(m);
		vector <int> min_b(m, 1e9);
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				int f;
				cin >> f;
				b[i].push_back(f);
				min_b[i] = min(min_b[i], f);
			}
		}
		int ind = -1;
		for (int i = 0; i < m; i++) {
			if (min_b[i] == min_first) {
				ind = i;
			}
		}
		vector <int> ans(n, -1);
		for (int i = 0; i < n; i++) {
			int k = b[ind][i];
			for (int j = 0; j < n; j++) {
				if (a[j][0] == k) {
					ans[i] = j;
				}
			}
		}
		for (int i = 0; i < n; i++) {
			int print = ans[i];
			for (auto i : a[print]) {
				cout << i << " ";
			}
			cout << endl;
		}

	}

}


#include <iostream>
#include <string>
using namespace std;

void print(int* arr, int n){
            for(int i =0; i<n;i++){
                        cout<<arr[i]<<" ";
            }
            cout<<"\n";
}

int main(){

            int n, h, w;
            int** rows, **cols;
            cin>>n;
            while(n--){
                        cin>>h>>w;
                        rows = new int* [h];
                        for(int i =0; i<h;i++){
                                    rows[i] = new int[w];
                                    for(int j =0; j<w; j++){
                                                cin>>rows[i][j];
                                    }
                        }
                        cols = new int* [w];
                        for(int i =0; i<w;i++){
                                    cols[i] = new int [h];
                                    for(int j =0; j<h; j++){
                                                cin>>cols[i][j];
                                    }
                        }

                       //----------------------------------------------------
                       for(int i = 0; i <h ; i++){

                                   for(int j =0; j<h; j++){
                                               for(int k =0; k<w; k++){
                                                           if (rows[j][k] == cols[0][i]){
                                                                      print(rows[j], w) ;
                                                           }
                                               }
                                   }
                       }
            }

            return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <deque>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = 1'000'000'007;

void Solve() {
	int n, m;
	cin >> n >> m;
	vector <pair <int, int>> c(n * m);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			int k;
			cin >> k;
			c[k - 1].second = j;
		}
	}
	for (int j = 0; j < m; j++) {
		for (int i = 0; i < n; i++) {
			int k;
			cin >> k;
			c[k - 1].first = i;
		}
	}

	vector <vector <int>> v(n, vector <int>(m));
	for (int i = 0; i < c.size(); i++) {
		v[c[i].first][c[i].second] = i + 1;
	}
	for (auto& i : v) {
		for (auto j : i)
			cout << j << " ";
		cout << '\n';
	}
}

void IOOptimize() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
}

int main() {
	IOOptimize();
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	Solve();
}


#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		int n, m;
		cin >> n >> m;
		map<int, int> hah;
		for (int j = 0; j < n; ++j) {
			for (int k = 0; k < m; ++k) {
				int x;
				cin >> x;
				hah[x] = k;
			}
		}
		vector<int> res(m);
		vector<vector<int>> desk(n, vector<int>(m));
		for (int j = 0; j < m; ++j) {
			int x;
			cin >> x;
			res[hah[x]] = j;
			desk[0][j] = x;
			for (int k = 1; k < n; ++k) {
				cin >> desk[k][j];
			}
		}
		for (int j = 0; j < n; ++j) {
			for (int k : res)
				cout << desk[j][k] << ' ';
			cout << '\n';
		}
	}
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void solve() {
	int n, m, k;
	cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m, 0));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> a[i][j];
		}
	}
	vector <int> l(n);
	for (int i = 0; i < m; ++i) {
		bool f = false;
		for (int j = 0; j < n; ++j) {
			cin >> l[j];
			if (a[0][0] == l[j]) {
				f = true;
			}
		}
		if (f) {
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < n; ++j) {
					if (l[i] == a[j][0]) {
						a[i].swap(a[j]);
						break;
					}
				}
			}
		}
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << a[i][j] << ' ';
		}
		cout << '\n';
	}
	return;
}

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		solve();
		cout << '\n';
	}
	return 0;
}


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

using namespace std;

int main()
{
	cin.tie();
	ios_base::sync_with_stdio(false);

	int t;
	cin >> t;
	while (t--)
	{
		int n,m;
		cin >> n >> m;
		vector<vector<int>> a(n);
		vector<vector<int>> aa = a;
		vector<int> d(m);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				cin >> d[j];
			}
			a[i] = d;
			sort(d.begin(), d.end());
			aa[i] = d;
		}

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

		vector<vector<int>> res(n);
		vector<int> dop;
		for (int i = 0; i < n; i++)
		{
			d.resize(0);
			for (int j = 0; j < m; j++)
			{
				dop = b[j];
				d.push_back(dop[i]);
			}
			sort(d.begin(), d.end());
			int lenaa = aa.size();
			for (int j = 0; j < lenaa; j++)
			{
				if (d == aa[j])
				{
					res[i] = a[j];
				}
			}
		}

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


#include <iostream>
#include <vector>
#include <map>

void solve(
    const std::vector< std::vector<int> >& rows,
    const std::vector< std::vector<int> >& cols,
    std::vector< std::vector<int> >& out
)
{
    int R = rows.size();
    int C = cols.size();

    // Find where the 1st element of rows[0] appears in cols.
    int special_col = -1;
    for ( int r = 0 ; r < rows.size() ; ++r ) {
        for ( int c = 0 ; c < cols.size() ; ++c ) {
            if ( cols[ c ][ r ] == rows[ 0 ][ 0 ] ) {
                special_col = c;
                break;
            }
        }
        if ( special_col >= 0 ) {
            break;
        }
    }

    // need inverse map for the special column.
    std::map<int, int> scol_inv;
    for ( int r = 0 ; r < R ; ++r ) {
        scol_inv[ cols[ special_col ][ r ] ] = r;
    }

    for ( int r = 0 ; r < R ; ++r ) {
        int idx = scol_inv[ rows[ r ][ 0 ] ];
        for ( int c = 0 ; c < C ; ++c ) {
            out[ idx ][ c ] = rows[ r ][ c ];
        }
    }
    return;
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie( NULL );
    std::cout.tie( NULL );

    int T;
    std::cin >> T;
    std::vector< std::vector<int> > rows;
    std::vector< std::vector<int> > out;
    std::vector< std::vector<int> > cols;
    int R, C;
    for ( int k = 0 ; k < T ; ++k ) {
        std::cin >> R >> C;
        int M = R;
        int N = C;
        rows.resize( M );
        out.resize( M );
        for ( int r = 0 ; r < M ; ++r ) {
            rows[ r ].resize( N );
            out[ r ].resize( N );
            for ( int c = 0 ; c < N ; ++c ) {
                std::cin >> rows[ r ][ c ];
            }
        }

        cols.resize( N );
        for ( int c = 0 ; c < N ; ++c ) {
            cols[ c ].resize( M );
            for ( int r = 0 ; r < M ; ++r ) {
                std::cin >> cols[ c ][ r ];
            }
        }
        solve(rows, cols, out);

        for ( int r = 0 ; r < R ; ++r ) {
            for ( int c = 0 ; c < C ; ++c ) {
                std::cout << out[r][c] << ' ';
            }
            std::cout << '\n';
        }
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <iomanip>
#include <set>
using namespace std;
pair<int,int> a[111111];
int ans[511][511];
void solve() {
	int n, m;
	cin >> n >> m;
	int pos = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			int k;
			cin >> k;
			a[k].second = j;
		}
	}
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			int k;
			cin >> k;
			a[k].first = j;
		}
	}
	int y = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			ans[a[y].first][a[y].second] = y;
			y++;
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
}
int main() {
	int t;
	cin >> t;
	for (int i = 1; i <= t; i++) {
		solve();
	}
}


#include <iostream>
#include <vector>
#include <iomanip>
#include <set>
using namespace std;
pair<int,int> a[1111111];
int ans[511][511];
void solve() {
	int n, m;
	cin >> n >> m;
	int pos = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			int k;
			cin >> k;
			a[k].second = j;
		}
	}
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			int k;
			cin >> k;
			a[k].first = j;
		}
	}
	int y = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			ans[a[y].first][a[y].second] = y;
			y++;
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
}
int main() {
	int t;
	cin >> t;
	for (int i = 1; i <= t; i++) {
		solve();
	}
}


#include <iostream>
using namespace std;

int main() {
    int t, a;
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a;
        if(360 % (180-a)) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a,b,c,d,e,f;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>b;
        if(360%(180-b)!=0){ cout<<"NO"<<endl;} else cout<<"YES"<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a,b,c,d,e,f;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>b;
        if(b<60||360%b!=0){ cout<<"NO";} else cout<<"YES";
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a,b,c,d,e,f;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>b;
        if(b<60||360%b!=0){ cout<<"NO"<<endl;} else cout<<"YES"<<endl;
    }
    return 0;
}


//#include "stdafx.h"

#include <iostream>
#include <string>

const int maxT = 180;

int main()
{
	using namespace std;

	int arrayVar[maxT];
	int nVar = 0;
	for(int n=3; n<720; n++)
	{
		double dn = n;
		double a = ((dn-2)/dn)*180;
		if(a == ((int)a))
		{
			//cout.width(5);
			//cout << n << " - " << a << endl;
			arrayVar[nVar] = a;
			nVar++;
		}
	}

	//for(int v=0; v<nVar; v++)
	//{
	//	cout << arrayVar[v] << endl;
	//}

	//85
	//108
	//119
	//return 0;

	int t;
	cin >> t;

	int arrayA[maxT];

	for(int a=0; a < t; a++)
	{
		cin >> arrayA[a];
	}

	string result;
	for(int a=0; a < t; a++)
	{
		result = "NO";
		for(int v=0; v<nVar; v++)
		{
			if(arrayA[a] == arrayVar[v])
			{
				result = "YES";
				break;
			}
		}

		cout << result << endl;
	}

	return 0;
}


//#include "stdafx.h"

#include <iostream>
#include <string>

const int maxT = 180;

int main()
{
	using namespace std;

	int arrayVar[maxT];
	int nVar = 0;
	for(int n=3; n<720; n++)
	{
		double dn = n;
		double a = ((dn-2)/dn)*180;
		if(a == ((int)a))
		{
			arrayVar[nVar] = a;
			nVar++;
		}
	}

	int t;
	cin >> t;

	int arrayA[maxT];

	for(int a=0; a < t; a++)
	{
		cin >> arrayA[a];
	}

	string result;
	for(int a=0; a < t; a++)
	{
		result = "NO";
		for(int v=0; v<nVar; v++)
		{
			if(arrayA[a] == arrayVar[v])
			{
				result = "YES";
			}
		}

		cout << result << endl;
	}

	return 0;
}


//#include "stdafx.h"

#include <iostream>
#include <string>

const int maxT = 180;

int main()
{
	using namespace std;

	int arrayVar[maxT];
	int nVar = 0;
	for(int n=3; n<720; n++)
	{
		double dn = n;
		double a = ((dn-2)/dn)*180;
		if(a == ((int)a))
		{
			arrayVar[nVar] = a;
			nVar++;
		}
	}

	for(int v=0; v<nVar; v++)
	{
		cout << arrayVar[v] << endl;
	}
	return 0;

	int t;
	cin >> t;

	int arrayA[maxT];

	for(int a=0; a < t; a++)
	{
		cin >> arrayA[a];
	}

	string result;
	for(int a=0; a < t; a++)
	{
		result = "NO";
		for(int v=0; v<nVar; v++)
		{
			if(arrayA[a] == arrayVar[v])
			{
				result = "YES";
			}
		}

		cout << result << endl;
	}

	return 0;
}


//#include "stdafx.h"

#include <iostream>
#include <string>

const int maxT = 180;

int main()
{
	using namespace std;

	//int arrayVar[maxT];
	//int nVar = 0;
	//for(int n=3; n<=360; n++)
	//{
	//	double dn = n;
	//	double a = ((dn-2)/dn)*180;
	//	if(a == ((int)a))
	//	{
	//		//cout.width(5);
	//		//cout << n << " - " << a << endl;
	//		arrayVar[nVar] = a;
	//		nVar++;
	//	}
	//}

	//for(int v=0; v<nVar; v++)
	//{
	//	cout << arrayVar[v] << endl;
	//}

	//85
	//108
	//119
	//return 0;

	int t;
	cin >> t;

	int arrayA[maxT];

	for(int a=0; a < t; a++)
	{
		cin >> arrayA[a];
	}

	string result;
	for(int a=0; a < t; a++)
	{
		/*result = "NO";
		for(int v=0; v<nVar; v++)
		{
			if(arrayA[a] == arrayVar[v])
			{
				result = "YES";
				break;
			}
		}*/
		result = "YES";
		if(360%(180-arrayA[a]))
		{
			result = "NO";
		}

		cout << result << endl;
	}

	return 0;
}


//#include "stdafx.h"

#include <iostream>
#include <string>

const int maxT = 180;

int main()
{
	using namespace std;

	int arrayVar[maxT];
	int nVar = 0;
	for(int n=3; n<180; n++)
	{
		double dn = n;
		double a = ((dn-2)/dn)*180;
		if(a == ((int)a))
		{
			arrayVar[nVar] = a;
			nVar++;
		}
	}

	int t;
	cin >> t;

	int arrayA[maxT];

	for(int a=0; a < t; a++)
	{
		cin >> arrayA[a];
	}

	string result;
	for(int a=0; a < t; a++)
	{
		result = "NO";
		for(int v=0; v<nVar; v++)
		{
			if(arrayA[a] == arrayVar[v])
			{
				result = "YES";
			}
		}

		cout << result << endl;
	}

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdio>
#include <bitset>
using namespace std;
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        if(360%(180-n)==0)
            cout<<"YES";
        else cout<<"NO";
        cout<<endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t, angle;

    cin >> t;

    for(int i = 1; i <= t; i++)
    {
        cin >> angle;

        if(360 % (180 - angle)) cout << "NO";
        else cout << "YES";
        cout << endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t, angle;

    cin >> t;

    for(int i = 1; i <= t; i++)
    {
        cin >> angle;

        if(360 % (180 - angle)) cout << "NO";
        else cout << "YES";
    }
}


#include <iostream>
#include <algorithm>

using namespace std;

class Polygon
{
	int t,n;
public:
	void calculation()
	{
		if(n >= 60 && 360 % (180 - n) == 0)
			cout << "YES\n";
		else
			cout << "NO\n";
	}
	void answer()
	{
		cin >> t;
		while(t--)
		{
			cin >> n;
			calculation();
		}
	}
};

int main()
{
	Polygon p;
	p.answer();
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
double a;
cin>>a;
double x =(360/(180-a));
int y=(int)x;
if(x==y)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return(0);
}


#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
double a;
cin>>a;
double x =(360/(180-a));
int y=(int)x;
if(x==y)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return(0);
}


#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <complex>
#include <utility>
#include <bitset>
#include <iomanip>
#include <ctime>

#define MohammadAmin main()
#define ll long long int
#define c_false ios_base::sync_with_stdio(false)

using namespace std;

int MohammadAmin{
    c_false;
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        cout << ( 360%(180-n) == 0 ? "YES" : "NO" ) << endl;
    }
	return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float q[200],x;
    int i,t,f;
    cin>>t;
    for(i=0;i<t;i++)
    cin>>q[i];
    for(i=0;i<t;i++)
    {
        f=100;
        if(q[i]<60&&q[i]>179)
        f=0;
        else
        {
            x=(float)360/(180-q[i]);
            if(floor(x)==ceil(x))
            f=1;
        }
        if(f==1)
        cout<<"YES\n";
        else
        cout<<"NO\n";
    }
    return 0;
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float q[200],x;
    int i,t,y;
    cin>>t;
    for(i=0;i<t;i++)
    cin>>q[i];
    for(i=0;i<t;i++)
    {
        if(q[i]>90)
        x=1;
        else if(q[i]==90)
        x=2;
        else
        {
            x=(float)180/(90-q[i]);
        }
        if(floor(x)==ceil(x))
        y=x;
        else
        y=1;
        if(y%2==0)
        cout<<"YES\n";
        else
        cout<<"NO\n";
    }
    return 0;
}


#include<stdio.h>

int main(){
	int t, angle, i, flag;
	scanf("%d", &t);
	int a[10] = {60, 90, 108, 120, 135, 140, 144, 150, 156, 160};

	while(t--){
		flag = 0;
		scanf("%d", &angle);
		if(angle == 30)	printf("NO\n");
		else{
		for(i = 0; i <= 10; i++){
			if(a[i] == angle){
				printf("YES\n");
				flag = 1;
				break;
			}
		}
		if(flag == 0)	printf("NO\n");
		}
	}
	return 0;
}


#include<stdio.h>
int main(){int t,a;scanf("%d",&t);while(t--&&scanf("%d",&a)){a=180-a;!(360%a)?puts("YES"):puts("NO");}return 0;}


#include <stdio.h>
#include <math.h>

 int main(){
     int i,t,a;
 scanf("%d",&t);
for(i=1;i<=t;i++){
    scanf("%d",&a);
    a=180-a;
    if(360%a==0){
        printf("YES");
    }
    else
        printf("NO");}
    return 0;}


#include <stdio.h>
#include <math.h>

 int main(){
     int i,t,a;
 scanf("%d",&t);
for(i=1;i<=t;i++){
    scanf("%d",&a);
    a=180-a;
    if(360%a==0){
        printf("YES\n");
    }
    else
        printf("NO\n");}
    return 0;}


#include <stdio.h>
#include <math.h>

 int main(){
     int i,t,a;
 scanf("%d",&t);
 int ara[t];
for(i=0;i<t;i++){
    scanf("%d",&ara[i]);}
for(i=0;i<t;i++){
    ara[i]=180-ara[i];
    if(360%ara[i]==0){
        printf("YES\n");
    }
    else
        printf("NO\n");}
    return 0;}


#include<stdio.h>
int main()
{
    int i,a,b,n,ans;
    scanf("%d",&a);
    for(i=0;i<a;i++){
        scanf("%d",&b);
        n=1;
        while(1){
            ans=((n-2)*180)/n;
            if(ans==b){
                printf("YES\n");
             break;
            }
            else if(ans>b){
                printf("NO\n");
                break;
            }
                n++;
        }
    }
}


#include<stdio.h>
int main()
{
    int i,a,b,n,ans;
    scanf("%d",&a);
    for(i=0;i<a;i++){
        scanf("%d",&b);
        n=3;
        while(1){
            ans=((n-2)*180)/n;
            if(ans==b&&((n-2)*180)%n==0){
                printf("%d\nYES\n",n);
             break;
            }
            else if(ans>b){
                printf("NO\n");
                break;
            }
                n++;
        }
    }
}


#include<stdio.h>
int main()
{
    int i,a,b,n,ans;
    scanf("%d",&a);
    for(i=0;i<a;i++){
        scanf("%d",&b);
        n=3;
        while(1){
            ans=((n-2)*180)/n;
            if(ans==b&&((n-2)*180)%n==0){
                printf("YES\n");
             break;
            }
            else if(ans>b){
                printf("NO\n");
                break;
            }
                n++;
        }
    }
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a,i;
float b;
scanf("%d",&a);
float s[a];
for(i=0;i<a;i++)
{
    scanf("%f",&s[i]);
}
for(i=0;i<a;i++)
{
    b=(float)(360/(180-s[i]));
    printf("%f",b);

    if((ceil(b)-b)==0)
        {printf("YES");}

    else
    {
        printf("NO");
    }

}
return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a,i;
float b;
scanf("%d",&a);
float s[a];
for(i=0;i<a;i++)
{
    scanf("%f",&s[i]);
}
for(i=0;i<a;i++)
{
    b=(float)(360/(180-s[i]));

    if((ceil(b)-b)==0)
        {printf("YES\n");}

    else
    {
        printf("NO\n");
    }

}
return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a,i;
float b;
scanf("%d",&a);
float s[a];
for(i=0;i<a;i++)
{
    scanf("%f",&s[i]);
}
for(i=0;i<a;i++)
{
    b=(float)(360/(180-s[i]));

    if((ceil(b)-b)==0)
        {printf("YES");}

    else
    {
        printf("NO");
    }

}
return 0;
}


#include<stdio.h>

int main(){

int t,i,j;

scanf("%d", &t);

int n[t];

for(j=0;j<t;j++)
    scanf("%d", &n[j]);

for(i=0;i<t;i++){
        if(n[i]>= 60)
            printf("YES\n");

        else
            printf("NO\n");

}

return 0;
}


# include <stdio.h>
int main()
{
    int i=1,j=3,t,a,count=0,c=1;
    scanf("&d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&a);
        for(j=3;j<=10000000;j++)
        {
            if(a%(180*(j-2)/j)==0)
               {
                  count++;break;

               }
            else continue;
        }
        if(count==1)
            printf("YES");
        else
            printf("NO");
    }
    return 0;
}


#include<stdio.h>
int main()
{
int t,n,a,i=0;
scanf("%d",&t);
for(i=0;i<t;i++)
{
    scanf("%d",&a);
    if(a==180)
    {
        printf("NO\n");
    }
    else
    {

       if(360%(180-a)==0)
       {
        printf("YES\n");
       }
       else
       {
        printf("NO\n");
       }
    }
}
return 0;
}


# include <stdio.h>
int main()
{
    int i=1,j=3,t,a,count=0;
    scanf("&d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&a);
        for(j=3;j<=10000000;j++)
        {
            if(a%(180*(j-2)/j)==0)
               {
                  count++;break;

               }
            else continue;
        }
        if(count==1)
            printf("YES");
        else
            printf("NO");
    }
    return 0;
}


# include <stdio.h>
int main()
{
    int i=1,j=3,t,a,count=0;
    scanf("&d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&a);
        for(j=3;j<=10000;j++)
        {
            if(a%(180*(j-2)/j)==0)
               {
                  count++;break;

               }
            else continue;
        }
        if(count==1)
            printf("YES");
        else
            printf("NO");
    }
    return 0;
}


# include <stdio.h>
int main()
{
    int i=1,j=3,t,a,count=0;
    scanf("&d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&a);
        for(j=3;j<=1000;j++)
        {
            if(a%(180*(j-2)/j)==0)
               {
                  count++;break;

               }
            else continue;
        }
        if(count==1)
            printf("YES");
        else
            printf("NO");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i,t;
    float a;
    float n;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
scanf("%f",&a);
n=360/(180-a);
if(a==0)
{
    printf("NO\n");
}
else if((n-(int)(n))!=0)
{
    printf("NO\n");
}
else
{
    printf("YES\n");
}
    }

return 0;
}


#include<stdio.h>
int main ()
{
    int t,a,i,count;
    double k,j;
    scanf("%d",&t);
    for(a=1;a<=t;a++)
    {
        count=0;
        scanf("%d",&i);
        for(j=3;j<=360;j++)
        {
           k=((j-2)*180)/j;
           if(k==i)
           {
                printf("YES\n");
                break;
           }
           else
           {
                count=1+count;
           }
        }

        if(count==(361-3))
        {
            printf("NO\n");
        }
    }

}


#include<stdio.h>
int main ()
{
    int t,a,i,j,count;
    double k;
    scanf("%d",&t);
    for(a=1;a<=t;a++)
    {
        count=0;
        scanf("%d",&i);
        for(j=3;j<=1000000;j++)
        {
           k=((j-2)*180)/j;
           if(k==i)
           {
                printf("YES");
                break;
           }
           else
           {
                count=1+count;
           }

        }
        if(count==(1000001-3))
        {
            printf("NO");
        }
    }
}


#include<stdio.h>
int main ()
{
    int t,a,i,j,count;
    double k;
    scanf("%d",&t);
    for(a=1;a<=t;a++)
    {
        count=0;
        scanf("%d",&i);
        for(j=3;j<=1000000;j++)
        {
           k=((j-2)*180)/j;
           if(k==i)
           {
                printf("YES\n");
                break;
           }
           else
           {
                count=1+count;
           }
        }

        if(count==(1000001-3))
        {
            printf("NO\n");
        }
    }

}


#include<stdio.h>
int main ()
{
    int t,a,i,j,count;
    int k;
    scanf("%d",&t);
    for(a=1;a<=t;a++)
    {
        count=0;
        scanf("%d",&i);
        for(j=3;j<=360;j++)
        {
           k=((j-2)*180)/j;
           if(k==i)
           {
                printf("YES\n");
                break;
           }
           else
           {
                count=1+count;
           }
        }

        if(count==(361-3))
        {
            printf("NO\n");
        }
    }

}


#include <stdio.h>
int main()
{
    double a,n;
    int x,T,i;
    scanf("%d",&T);
    for(i=1; i<=T; i++)
    {
        scanf("%lf",&a);
        n=(360/(180-a));
        x=n;
        if(n>2 && n==x)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
int main()
{
    double a,n;
    int x,T,i;
    scanf("%d",&T);
    for(i=0;i<=T;i++)
    {
        scanf("%lf",&a);
        n=(360/(180-a));
        x=n;
        if(n>2 && n==x)
            printf("YES");
        else
            printf("NO");
    }
    return 0;
}


#include <stdio.h>
int main()
{
    double a,n;
    int x,T,i;
    scanf("%d",&T);
    for(i=0; i<=T; i++)
    {
        scanf("%lf",&a);
        n=(360/(180-a));
        x=n;
        if(n>2 && n==x)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
int main()
{
    double a,n;
    int x,T,i;
    scanf("%d",&T);
    for(i=0;i<=T;i++)
    {
        scanf("%lf",&a);
        n=(360/(180-a));
        x=n;
        if(n>2 && n==x)
            printf("YES");
        else
            printf("NO");
        printf("\n");
    }
    return 0;
}


#include<stdio.h>
main()
{
	int n,i;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)
{
	scanf("%d",&a[i]);
	if(a[i]>=60 && a[i]<180)
	printf("YES\n");
	else
	printf("N0\n");
}
}


#include<stdio.h>
main()
{
	int n,i;
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++)
{
	scanf("%d",&a[i]);
	if(a[i]>=60 && a[i]<180)
	printf("YES\n");
	else
	printf("NO\n");
}
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
    double t,n,x,a0,alpha,z,angle,f,beta,checker;
    scanf("%lf",&t);
    for(a0=0;a0<t;a0++)
    {
        scanf("%lf",&angle);
        checker=fmod(180,angle);
        if(checker==0.0)
        {
            z=x-1;
            alpha=x*2;
            f=fmod(alpha,z);
            if(f==0.0)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n",f);
            }
        }
        else
        {
            beta=180-angle;
            alpha=180*2;
            f=fmod(alpha,beta);
            if(f==0.0)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
    }
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
    double t,n,x,a0,alpha,z,angle,f;
    scanf("%lf",&t);
    for(a0=0;a0<t;a0++)
    {
        scanf("%lf",&angle);
        x=180/angle;
        z=x-1;
        alpha=x*2;
        f=fmod(alpha,z);
        if(f==0.0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
    double t,n,x,a0,alpha,z,angle,f,beta,checker;
    scanf("%lf",&t);
    for(a0=0;a0<t;a0++)
    {
        scanf("%lf",&angle);
        checker=fmod(180,angle);
        if(checker==0.0)
        {
            z=x-1;
            alpha=x*2;
            f=fmod(alpha,z);
            if(f==0.0)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO %lf\n",f);
            }
        }
        else
        {
            beta=180-angle;
            alpha=180*2;
            f=fmod(alpha,beta);
            if(f==0.0)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
    }
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
    double t,n,x,a0,alpha,z,angle,f,beta,checker;
    scanf("%lf",&t);
    for(a0=0;a0<t;a0++)
    {
        scanf("%lf",&angle);
        checker=fmod(180,angle);
        if(checker==0.0)
        {
            x=180/angle;
            z=x-1;
            alpha=x*2;
            f=fmod(alpha,z);
            if(f==0.0)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
        else
        {
            beta=180-angle;
            alpha=180*2;
            f=fmod(alpha,beta);
            if(f==0.0)
            {
                printf("YES\n");
            }
            else
            {
                printf("NO\n");
            }
        }
    }
}


#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main()
{
    double t,n,x,a0,alpha,z,angle;
    int f;
    scanf("%lf",&t);
    for(a0=0;a0<t;a0++)
    {
        scanf("%lf",&angle);
        x=180/angle;
        z=x-1;
        alpha=x*2;
        f=fmod(alpha,z);
        if(f==0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
}


#include<stdio.h>
int main()
{
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {int a;
    scanf("%d",&a);
    if(!(360%(180-a))) printf("YES\n");
    else printf("NO\n");
    return 0;
}


#include<stdio.h>
int main()
{
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {int a;
    scanf("%d",&a);
    if(!(360%(180-a))) printf("YES\n");
    else printf("NO\n");}
    return 0;
}


#include<stdio.h>
int main()
{
    int tc;
    scanf("%d",%tc);
    while(tc--)
    {int a;
    scanf("%d",&a);
    if(!(360/(180-a))) printf("YES\n");
    else printf("NO\n");}
    return 0;
}


#include<stdio.h>
int main()
{
    int tc;
    scanf("%d",%tc);
    while(tc--)
    {int a;
    scanf("%d",&a);
    if(!(360/(180-a))) printf("YES\n");
    else printf("NO\n");
    return 0;
}


#include<stdio.h>
int main()
{
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {int a;
    scanf("%d",&a);
    if(!(360/(180-a))) printf("YES\n");
    else printf("NO\n");}
    return 0;
}


#include <stdio.h>
int main(){
    int t, i, j, n,y,x;
    scanf("%d",&t);
    for (i=0;i<t;i++){
        scanf("%d",&n);
        y=0;
        for (j=3;j<20;j++){
            x=(j-2)*180/j;
            if (n==x){
                printf("YES\n");
                y=1;
                break;
            }
        }
        if (y==0)
        printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
int main(){
    int t, i, j, n,y,x;
    scanf("%d",&t);
    for (i=0;i<t;i++){
        scanf("%d",&n);
        y=0;
        for (j=3;j<180;j++){
            x=(j-2)*180/j;
            if (n==x){
                printf("YES\n");
                y=1;
                break;
            }
        }
        if (y==0)
        printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    double pol,n,i,j,angle;
    scanf("%lf",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf",&angle);
        if(angle<60)
            printf("NO\n");
        else
        {
            pol=0;
            for(j=3;pol<angle;j++)
        {
            pol=(((j-2)*180)/j);
            printf("j= %lf, pol =%lf\n",j,pol);
            if(pol==angle)
            {
                printf("YES\n");
                break;
            }
             if(pol>angle)
                printf("NO\n");
        }
        }

    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,angle,i,j,pol;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&angle);
        if(angle<60)
            printf("NO\n");
        else
        {
            pol=0;
            for(j=3;pol<angle;j++)
        {
            pol=((j-2)*180)/j;
            if(pol==angle)
            {
                printf("YES\n");
                break;
            }
             if(pol>angle)
                printf("NO\n");
        }
        }

    }
    return 0;
}


#include<stdio.h>
int main()
{
    double pol,n,i,j,angle;
    scanf("%lf",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf",&angle);
        if(angle<60)
            printf("NO\n");
        else
        {
            pol=0;
            for(j=3;pol<angle;j++)
        {
            printf("j= %lf, pol =%lf\n",j,pol);
            if(pol==angle)
            {
                printf("YES\n");
                break;
            }
             if(pol>angle)
                printf("NO\n");
        }
        }

    }
    return 0;
}


#include<stdio.h>
int main()
{
    double pol,n,i,j,angle;
    scanf("%lf",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lf",&angle);
        if(angle<60)
            printf("NO\n");
        else
        {
            pol=0;
            for(j=3;pol<angle;j++)
        {
            pol=(((j-2)*180)/j);
            if(pol==angle)
            {
                printf("YES\n");
                break;
            }
             if(pol>angle)
                printf("NO\n");
        }
        }

    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,i;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
    float n,b,c,e;
    int a,d;
    scanf("%d",&a);
    b=(float)180-a;
    c=360/b;
    d=(int)c;
    e=c-d;
    if(e==0)
    {
        printf("YES\n");
    }
    else
        printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int t,i;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
    float n,b,c,e;
    int a,d;
    scanf("%d",&a);
    b=(float)180-a;
    c=360/b;
    d=(int)c;
    e=c-d;
    if(e==0)
    {
        printf("YES\n");
    }
    else
        printf("NO");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int cas_e,i,angle[181];
    scanf("%d",&cas_e);//test case
    for(i=0;i<cas_e;i++)
       scanf("%d",&angle[i]);//input angle
    for(i=0;i<cas_e;i++){
       if(angle[i]>=60&&90>=angle[i])
       printf("YES\n");
       else
       printf("NO\n");
     }
return 0;
}


#include<stdio.h>
int main()
{
    int n,i,a[180],b;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
    }
    b=a[0];
    if(b==a[0])
        printf("NO\n");
    for(i=1;i<n;i++)
    {
       if(a[i]%b==0)
       {
       printf("YES\n");
       }
       else
        printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int cas_e,i,angle[181];
    scanf("%d",&cas_e);//test case
    for(i=0;i<cas_e;i++)
       scanf("%d",&angle[i]);//input angle
    for(i=0;i<cas_e;i++){
       if(angle[i]>=60)
       printf("YES\n");
       else
       printf("NO\n");
     }
return 0;
}


#include<stdio.h>

int main()
{
    int t,n,i,count=0;;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(i=3;i<300;i++)
        {
            if((((2*i-4)*90)/i)==n)
            {
                count++;
                break;
            }
        }
        if(count>0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>

int main()
{
    int t,n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        if((360%(180-n))==0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>

int main()
{
    int t,n,i,count=0;;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        if((360%(180-n))==0)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main ()
{
    int flag=0,i,j,y;
    double a,t,x;
    scanf("%lf",&t);
    for(i=0;i<t;i++)
    {
    scanf("%lf",&a);
    x=360/(180-a);
    y=x/1;
    if(y==x)printf("YES\n");
    else printf("NO\n");

    }

}


#include<stdio.h>
int main ()
{
    int flag=0,i,j,y;
    double a,t,x;
    scanf("%lf",&t);
    for(i=0;i<t;i++)
    {
    scanf("%lf",&a);
    x=360/(180-a);
    y=360/(180-a);
    if(y==x)printf("YES\n");
    else printf("NO\n");

    }

}


#include<stdio.h>
int main ()
{
    int flag=0,i,j,y;
    double a,t,x;
    scanf("%lf",&t);
    for(i=0;i<t;i++)
    {
    scanf("%lf",&a);
    x=360/(180-a);
    y=x/1;
    if(y==x)printf("YES");
    else printf("NO");

    }

}


#include<stdio.h>
int main ()
{
    int flag=0,t,a,i,x,j;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
    scanf("%d",&a);
    flag=0;
    for(j=3;j<=360;j++)
    {
        x=((j-2)*180)/j;
        if(a==x)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)printf("YES\n");
    else printf("NO\n");

    }

}


#include <stdio.h>

int main(void)
{
   int t,a,i,n;
    scanf("%d", &t);
   for(i=0;i<t;i++)
   {
       scanf("%d", &a);

       if( (360 / (180 - a) ) % 1 == 0 && a*3 >= 180)
        printf("YES\n");
        else
        printf("NO\n");
   }

}


#include <stdio.h>

int main(void)
{
   int t,a,i;
    scanf("%d", &t);
   for(i=0;i<t;i++)
   {
       scanf("%d", &a);
       if( a*3 >= 180)
        printf("YES\n");
        else
        printf("NO\n");
   }

}


#include <stdio.h>

int main(void)
{
   int t,a,i,n;
    scanf("%d", &t);
   for(i=0;i<t;i++)
   {
       scanf("%d", &a);

       if( (360 % (180 - a) ) == 0 && a*3 >= 180)
        printf("YES\n");
        else
        printf("NO\n");
   }

}


#include <stdio.h>

int main(void)
{
   int t,a,i;
    scanf("%d", &t);
   for(i=0;i<t;i++)
   {
       scanf("%d", &a);
       if( a*3 >= 180 && 180 % a == 0)
        printf("YES\n");
        else
        printf("NO\n");
   }

}


#include <stdio.h>

int main(void)
{
   int t,a,i;
    scanf("%d", &t);
   for(i=0;i<t;i++)
   {
       scanf("%d", &a);
       if( a*3 >= 180 && 180 % a == 0)
        printf("YES\n");
        else
        printf("No\n");
   }

}


#include<stdio.h>
int main()
{
int t,f=0,a,i;
double j;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&a);

for(j=3;j<400;j++)
	if (((j-2)*180)/j == a)
	{printf("YES\n");f++;break;}
	if (!f)
		printf("NO\n");
	f=0;
}

return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

const ll inf = 1000000000000000000ll;
const int N = 1e5 + 10;
typedef pair<ll, ll> PLL;
int b[N], a[N];
struct Segment {
#define lson p << 1
#define rson p << 1 | 1
    int l[N << 2], r[N << 2];
    ll sum[2][N << 2], dat[N << 2];
    int first;
    void push_up(int p)
    {
        sum[0][p] = sum[0][lson] + sum[0][rson];
        sum[1][p] = sum[1][lson] + sum[1][rson];
    }

    void build(int p, int L, int R)
    {
        l[p] = L, r[p] = R;
        sum[0][p] = sum[1][p] = 0;
        if (L == R) {
            if (L == 1)
                first = p;
            dat[p] = b[L];
            if (dat[p] >= 0)
                sum[0][p] = dat[p];
            else
                sum[1][p] = dat[p];
            return;
        }
        int mid = (L + R) >> 1;
        build(lson, L, mid);
        build(rson, mid + 1, R);
        push_up(p);
    }

    void change(int p, int x, ll v)
    {
        if (l[p] == r[p]) {
            sum[dat[p] < 0][p] = 0;
            dat[p] += v;
            sum[dat[p] < 0][p] = dat[p];
            return;
        }
        int mid = (l[p] + r[p]) >> 1;
        if (x <= mid)
            change(lson, x, v);
        else
            change(rson, x, v);
        push_up(p);
    }

    PLL query(int p, int L, int R)
    {
        if (L <= l[p] && r[p] <= R) {
            return make_pair(sum[0][p], sum[1][p]);
        }

        int mid = (l[p] + r[p]) >> 1;
        PLL ans = { 0, 0 };
        if (L <= mid) {
            PLL temp = query(lson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        if (R > mid) {
            PLL temp = query(rson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        return ans;
    }

} T;

int n, m;

ll solve(ll x, ll y)
{
    ll l = -inf, r = inf;
    while (l < r) {
        ll mid = (l + r) >> 1;
        ll temp = T.dat[T.first] - mid;
        if (temp + x <= mid)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}

int main()
{

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        b[i] = a[i] - a[i - 1];
    }
    T.build(1, 1, n);
    ll xx = T.sum[0][1], yy = T.sum[1][1];
    ll first = T.dat[T.first];
    if (first >= 0)
        xx = T.sum[0][1] - first;
    else
        yy = T.sum[1][1] - first;
    ll ans = solve(xx, yy);

    printf("%lld\n", ans);

    scanf("%d", &m);
    while (m--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        T.change(1, l, x);
        if (r + 1 <= n)
            T.change(1, r + 1, -x);
        PLL res = T.query(1, 1, n);
        xx = T.sum[0][1], yy = T.sum[1][1];
        first = T.dat[T.first];
        if (first >= 0)
            xx = res.first - first;
        else
            yy = res.second - first;
        ans = solve(xx, yy);
        printf("%lld\n", ans);
    }
    return 0;
}

/*

*/


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

const ll inf = 1000000000000000000ll;
const int N = 1e5 + 10;
typedef pair<ll, ll> PLL;
int b[N], a[N];
struct Segment {
#define lson p << 1
#define rson p << 1 | 1
    int l[N << 2], r[N << 2];
    ll sum[2][N << 2], dat[N << 2];
    int first;
    void push_up(int p)
    {
        sum[0][p] = sum[0][lson] + sum[0][rson];
        sum[1][p] = sum[1][lson] + sum[1][rson];
    }

    void build(int p, int L, int R)
    {
        l[p] = L, r[p] = R;
        sum[0][p] = sum[1][p] = 0;
        if (L == R) {
            if (L == 1)
                first = p;
            dat[p] = b[L];
            if (dat[p] >= 0)
                sum[0][p] = dat[p];
            else
                sum[1][p] = dat[p];
            return;
        }
        int mid = (L + R) >> 1;
        build(lson, L, mid);
        build(rson, mid + 1, R);
        push_up(p);
    }

    void change(int p, int x, int v)
    {
        if (l[p] == r[p]) {
            sum[dat[p] < 0][p] = 0;
            dat[p] += v;
            sum[dat[p] < 0][p] = dat[p];
            return;
        }
        int mid = (l[p] + r[p]) >> 1;
        if (x <= mid)
            change(lson, x, v);
        else
            change(rson, x, v);
        push_up(p);
    }

    PLL query(int p, int L, int R)
    {
        if (L <= l[p] && r[p] <= R) {
            return make_pair(sum[0][p], sum[1][p]);
        }

        int mid = (l[p] + r[p]) >> 1;
        PLL ans = { 0, 0 };
        if (L <= mid) {
            PLL temp = query(lson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        if (R > mid) {
            PLL temp = query(rson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        return ans;
    }

} T;

int n, m;

ll solve(ll x, ll y)
{
    ll l = -inf, r = inf;
    while (l < r) {
        ll mid = (l + r) >> 1;
        ll temp = T.dat[T.first] - mid;
        if (temp + x <= mid)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}

int main()
{

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        b[i] = a[i] - a[i - 1];
    }
    T.build(1, 1, n);
    int xx = T.sum[0][1], yy = T.sum[1][1];
    int first = T.dat[T.first];
    if (first >= 0)
        xx = T.sum[0][1] - first;
    else
        yy = T.sum[0][1] - first;
    ll ans = solve(xx, yy);

    printf("%lld\n", ans);

    scanf("%d", &m);
    while (m--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        T.change(1, l, x);
        if (r + 1 <= n)
            T.change(1, r + 1, -x);
        PLL res = T.query(1, 1, n);
        xx = T.sum[0][1], yy = T.sum[1][1];
        first = T.dat[T.first];
        if (first >= 0)
            xx = res.first - first;
        else
            yy = res.second - first;
        ans = solve(xx, yy);
        printf("%lld\n", ans);
    }
    return 0;
}

/*

*/


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

const ll inf = 1000000000000000000ll;
const int N = 1e5 + 10;
typedef pair<ll, ll> PLL;
int b[N], a[N];
struct Segment {
#define lson p << 1
#define rson p << 1 | 1
    int l[N << 2], r[N << 2];
    ll sum[2][N << 2], dat[N << 2];
    int first;
    void push_up(int p)
    {
        sum[0][p] = sum[0][lson] + sum[0][rson];
        sum[1][p] = sum[1][lson] + sum[1][rson];
    }

    void build(int p, int L, int R)
    {
        l[p] = L, r[p] = R;
        sum[0][p] = sum[1][p] = 0;
        if (L == R) {
            if (L == 1)
                first = p;
            dat[p] = b[L];
            if (dat[p] >= 0)
                sum[0][p] = dat[p];
            else
                sum[1][p] = dat[p];
            return;
        }
        int mid = (L + R) >> 1;
        build(lson, L, mid);
        build(rson, mid + 1, R);
        push_up(p);
    }

    void change(int p, int x, ll v)
    {
        if (l[p] == r[p]) {
            sum[dat[p] < 0][p] = 0;
            dat[p] += v;
            sum[dat[p] < 0][p] = dat[p];
            return;
        }
        int mid = (l[p] + r[p]) >> 1;
        if (x <= mid)
            change(lson, x, v);
        else
            change(rson, x, v);
        push_up(p);
    }

    PLL query(int p, int L, int R)
    {
        if (L <= l[p] && r[p] <= R) {
            return make_pair(sum[0][p], sum[1][p]);
        }

        int mid = (l[p] + r[p]) >> 1;
        PLL ans = { 0, 0 };
        if (L <= mid) {
            PLL temp = query(lson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        if (R > mid) {
            PLL temp = query(rson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        return ans;
    }

} T;

int n, m;

ll solve(ll x, ll y)
{
    ll l = -inf, r = inf;
    while (l < r) {
        ll mid = (l + r) >> 1;
        ll temp = T.dat[T.first] - mid;
        if (temp + x <= mid)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}

int main()
{

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        b[i] = a[i] - a[i - 1];
    }
    T.build(1, 1, n);
    int xx = T.sum[0][1], yy = T.sum[1][1];
    ll first = T.dat[T.first];
    if (first >= 0)
        xx = T.sum[0][1] - first;
    else
        yy = T.sum[1][1] - first;
    ll ans = solve(xx, yy);

    printf("%lld\n", ans);

    scanf("%d", &m);
    while (m--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        T.change(1, l, x);
        if (r + 1 <= n)
            T.change(1, r + 1, -x);
        PLL res = T.query(1, 1, n);
        xx = T.sum[0][1], yy = T.sum[1][1];
        first = T.dat[T.first];
        if (first >= 0)
            xx = res.first - first;
        else
            yy = res.second - first;
        ans = solve(xx, yy);
        printf("%lld\n", ans);
    }
    return 0;
}

/*

*/


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

const ll inf = 1000000000000000000ll;
const int N = 1e5 + 10;
typedef pair<ll, ll> PLL;
int b[N], a[N];
struct Segment {
#define lson p << 1
#define rson p << 1 | 1
    int l[N << 2], r[N << 2];
    ll sum[2][N << 2], dat[N];
    int first;
    void push_up(int p)
    {
        sum[0][p] = sum[0][lson] + sum[0][rson];
        sum[1][p] = sum[1][lson] + sum[1][rson];
    }

    void build(int p, int L, int R)
    {
        l[p] = L, r[p] = R;
        sum[0][p] = sum[1][p] = 0;
        if (L == R) {
            if (L == 1)
                first = p;
            dat[p] = b[L];
            if (dat[p] >= 0)
                sum[0][p] = dat[p];
            else
                sum[1][p] = dat[p];
            return;
        }
        int mid = (L + R) >> 1;
        build(lson, L, mid);
        build(rson, mid + 1, R);
        push_up(p);
    }

    void change(int p, int x, int v)
    {
        if (l[p] == r[p]) {
            sum[dat[p] < 0][p] = 0;
            dat[p] += v;
            sum[dat[p] < 0][p] = dat[p];
            return;
        }
        int mid = (l[p] + r[p]) >> 1;
        if (x <= mid)
            change(lson, x, v);
        else
            change(rson, x, v);
        push_up(p);
    }

    PLL query(int p, int L, int R)
    {
        if (L <= l[p] && r[p] <= R) {
            return make_pair(sum[0][p], sum[1][p]);
        }

        int mid = (l[p] + r[p]) >> 1;
        PLL ans = { 0, 0 };
        if (L <= mid) {
            PLL temp = query(lson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        if (R > mid) {
            PLL temp = query(rson, L, R);
            ans.first += temp.first;
            ans.second += temp.second;
        }
        return ans;
    }

} T;

int n, m;

ll solve(ll x, ll y)
{
    ll l = -inf, r = inf;
    while (l < r) {
        ll mid = (l + r) >> 1;
        ll temp = T.dat[T.first] - mid;
        if (temp + x <= mid)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}

int main()
{

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        b[i] = a[i] - a[i - 1];
    }
    T.build(1, 1, n);
    int xx = T.sum[0][1], yy = T.sum[1][1];
    int first = T.dat[T.first];
    if (first >= 0)
        xx = T.sum[0][1] - first;
    else
        yy = T.sum[0][1] - first;
    ll ans = solve(xx, yy);

    printf("%lld\n", ans);

    scanf("%d", &m);
    while (m--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        T.change(1, l, x);
        if (r + 1 <= n)
            T.change(1, r + 1, -x);
        PLL res = T.query(1, 1, n);
        xx = T.sum[0][1], yy = T.sum[1][1];
        first = T.dat[T.first];
        if (first >= 0)
            xx = res.first - first;
        else
            yy = res.second - first;
        ans = solve(xx, yy);
        printf("%lld\n", ans);
    }
    return 0;
}

/*

*/