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;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return (a > b && (a = b, true)); }
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return (a < b && (a = b, true)); }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
	os << v[0];
	for (int i = 1; i < v.size(); i++) os << " " << v[i];
	return (os);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
	for (auto &u : v) is >> u; return (is);
}

template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
	return (is >> p.first >> p.second);
}


void Main() {
	int N; cin >> N;
	vint S(N); cin >> S;
	vint T(N); cin >> T;
	vector<ull> U(N); cin >> U;
	vector<ull> V(N); cin >> V;
	vector<vector<ull>> ans(N, vector<ull>(N, 0));
	
	for (ull b = 0; b < 64; b++) {
		
		vvint C(N, vint(2, 0));
		vvint R(N, vint(2, 0));
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				ull valr = (U[i]>>b)&1, valc = (V[j]>>b)&1;
				if (S[i] == 0 && valr == 1) ans[i][j] |= 1llu << b;
				else if (T[j] == 0 && valc == 1) ans[i][j] |= 1llu << b;
				else if (valr == valc) ans[i][j] |= (valr << b);
				R[i][(ans[i][j]>>b)&1]++;
				C[j][(ans[i][j]>>b)&1]++;
			}
		}

		for (int i = 0; i < N; i++) {
			ull valr = (U[i]>>b)&1;
			if (!valr || !S[i]) continue;
			if (R[i][1]) continue;
			int j;
			for (j = 0; j < N; j++) {
				ull valc = (V[j]>>b)&1;
				if (T[j] == 0 && valc == 0 && C[j][0] > 1) {
					ans[i][j] |= (1llu<<b);
					//R[i][0]--;
					C[j][0]--;
					//R[i][1]++;
					//C[j][1]++;
					break;
				}
			}
		}

		for (int j = 0; j < N; j++) {
			ull valc = (V[j]>>b)&1;
			if (!valc || !T[j]) continue;
			if (C[j][1]) continue;
			int i;
			for (i = 0; i < N; i++) {
				ull valr = (U[i]>>b)&1;
				if (S[i] == 0 && valr == 0 && R[i][0] > 1) {
					ans[i][j] |= (1llu<<b);
					R[i][0]--;
					//C[j][0]--;
					//R[i][1]++;
					//C[j][1]++;
					break;
				}
			}
		}
	}
	
	for (int i = 0; i < N; i++) {
		ull x = 0;
		if (S[i] == 0) x = numeric_limits<ull>::max();
		for (int j = 0; j < N; j++) {
			if (S[i]) x |= ans[i][j];	
			else x &= ans[i][j];
		}
		if (x != U[i]) { cout << -1 << el; return; }
	}

	for (int j = 0; j < N; j++) {
		ull x = 0;
		if (T[j] == 0) x = numeric_limits<ull>::max();
		for (int i = 0; i < N; i++) {
			if (T[j]) x |= ans[i][j];	
			else x &= ans[i][j];
		}
		if (x != V[j]) { cout << -1 << el; return; }
	}

	for (int i = 0; i < N; i++) {
		cout << ans[i][0];
		for (int j = 1; j < N; j++) {
			cout << " " << ans[i][j];
		}
		cout << el;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout << fixed << setprecision(20);
	Main();
	return (0);
}