Problem: Given are an integer N and arrays S, T, U, and V, each of length N. Construct an N×N matrix a that satisfy the following conditions:

* a_{i,j} is an integer.
* 0 \leq a_{i,j} \lt 2^{64}.
* If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.
* If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.
* If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.
* If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.



However, there may be cases where no matrix satisfies the conditions.

Constraints

* All values in input are integers.
* 1 \leq N \leq 500
* 0 \leq S_{i} \leq 1
* 0 \leq T_{i} \leq 1
* 0 \leq U_{i} \lt 2^{64}
* 0 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:


N
S_{1} S_{2} ...  S_{N}
T_{1} T_{2} ...  T_{N}
U_{1} U_{2} ...  U_{N}
V_{1} V_{2} ...  V_{N}


Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:


a_{1,1} ...  a_{1,N}
:
a_{N,1} ...  a_{N,N}


Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Examples

Input

2
0 1
1 0
1 1
1 0


Output

1 1
1 0


Input

2
1 1
1 0
15 15
15 11


Output

15 11
15 11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;

#define SZ(v) ((int)(v).size())
using ll = unsigned long long;

const int MAXN = 500;
const ll MAXP = 64;

ll mat[MAXN][MAXN];
bool cond_lig[MAXN];
bool cond_col[MAXN];
ll val_lig[MAXN];
ll val_col[MAXN];
int N;

ll or_line(int line)
{
	ll ret(0);
	for (int i(0); i < N; ++i)
		ret |= mat[line][i];
	return ret;
}

ll and_line(int line)
{
	ll ret(mat[line][0]);
	for (int i(0); i < N; ++i)
		ret &= mat[line][i];
	return ret;
}

ll or_col(int col)
{
	ll ret(0);
	for (int i(0); i < N; ++i)
		ret |= mat[i][col];
	return ret;
}

ll and_col(int col)
{
	ll ret(mat[0][col]);
	for (int i(0); i < N; ++i)
		ret &= mat[i][col];
	return ret;
}

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

	cin >> N;
	for (int i(0); i < N; ++i)
		cin >> cond_lig[i];
	for (int i(0); i < N; ++i)
		cin >> cond_col[i];
	for (int i(0); i < N; ++i)
		cin >> val_lig[i];
	for (int i(0); i < N; ++i)
		cin >> val_col[i];

	for (ll p(0); p < MAXP; ++p)
	{
		ll bit = (1ULL<<p);
		int lig0f(0), lig0a(0), lig1f(0), lig1a(0);
		int col0f(0), col0a(0), col1f(0), col1a(0);
		for (int lig(0); lig < N; ++lig)
		{
			if (cond_lig[lig])
			{
				if (bit&val_lig[lig])
					lig1a++;
				else
					lig0f++;
			}
			else
			{
				if (bit&val_lig[lig])
					lig1f++;
				else
					lig0a++;
			}
		}
		for (int col(0); col < N; ++col)
		{
			if (cond_col[col])
			{
				if (bit&val_col[col])
					col1a++;
				else
					col0f++;
			}
			else
			{
				if (bit&val_col[col])
					col1f++;
				else
					col0a++;
			}
		}
		if ((col1f and lig0f) or (col0f and lig1f))
		{
			cout << -1 << endl;
			return 0;
		}
		for (int lig(0); lig < N; ++lig)
			if (!cond_lig[lig] and (bit&val_lig[lig]))
				for (int col(0); col < N; ++col)
					mat[lig][col] |= bit;
		for (int col(0); col < N; ++col)
			if (!cond_col[col] and (bit&val_col[col]))
				for (int lig(0); lig < N; ++lig)
					mat[lig][col] |= bit;
		if ((lig1a or lig1f) and (col1a or col1f))
		{
			for (int lig(0); lig < N; ++lig)
				for (int col(0); col < N; ++col)
					if ((bit&val_lig[lig]) and (bit&val_col[col]))
						mat[lig][col] |= bit;
		}	
		else if (lig1a)
		{
			bool fst(true);
			vector<int> col_can;
			for (int col(0); col < N; ++col)
				if (!cond_col[col] and !(bit&val_col[col]))
					col_can.push_back(col);
			if (SZ(col_can) == 0)
			{
				cout << -1 << endl;
				return 0;
			}
			if (SZ(col_can)==1)
			{
				for (int lig(0); lig < N; ++lig)
					if (cond_lig[lig] and (bit&val_lig[lig]))
						mat[lig][col_can[0]] |= bit;
			}
			else
			{
				for (int lig(0); lig < N; ++lig)
				{
					if (cond_lig[lig] and (bit&val_lig[lig]))
					{
						if (fst)
						{
							mat[lig][col_can[0]] |= bit;
							fst = false;
						}
						else
							mat[lig][col_can[1]] |= bit;
					}
				}
			}
		}
		else if (col1a)
		{
	
			bool fst(true);
			vector<int> lig_can;
			for (int lig(0); lig< N; ++lig)
				if (!cond_lig[lig] and !(bit&val_lig[lig]))
					lig_can.push_back(lig);
			if (SZ(lig_can) == 0)
			{
				cout << -1  << endl;
				return 0;
			}
			if (SZ(lig_can)==1)
			{
				for (int col(0); col< N; ++col)
					if (cond_col[col] and (bit&val_col[col]))
						mat[lig_can[0]][col] |= bit;
			}
			else
			{
				for (int col(0); col< N; ++col)
				{
					if (cond_col[col] and (bit&val_col[col]))
					{
						if (fst)
						{
							mat[lig_can[0]][col] |= bit;
							fst = false;
						}
						else
							mat[lig_can[1]][col] |= bit;
					}
				}
			}
		}
	}
	
	for (int lig(0); lig < N; ++lig)
	{
		if (cond_lig[lig])
		{
			if (or_line(lig) != val_lig[lig])
			{
				cout << -1 << endl;
				return 0;
			}
		}
		else
		{
			if (and_line(lig) != val_lig[lig])
			{
				cout << -1 << endl;
				return 0;
			}
		}
	}
	for (int col(0); col < N; ++col)
	{
		if (cond_col[col])
		{
			if (or_col(col) != val_col[col])
			{
				cout << -1 << endl;
				return 0;
			}
		}
		else
		{
			if (and_col(col) != val_col[col])
			{
				cout << -1 << endl;
				return 0;
			}
		}
	}
	for (int lig(0); lig < N; ++lig)
		for (int col(0); col < N; ++col)
			cout << mat[lig][col] << " \n"[col==N-1];
}