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:
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long int ll;

int s[550];
int t[550];

ll res[550][550];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			res[i][j]=0;
		}
	}
	for(int i=0;i<n;i++){
		cin >> s[i];
	}
	for(int i=0;i<n;i++){
		cin >> t[i];
	}
	vector<ll> u(n),v(n);
	for(int i=0;i<n;i++){
		cin >> u[i];
	}
	for(int i=0;i<n;i++){
		cin >> v[i];
	}
	for(int i=0;i<n;i++){
		if(s[i]==0){
			for(int j=0;j<n;j++){
				res[i][j]|=u[i];
				if(t[j]==1){
					if((v[j]|res[i][j])!=v[j]){
						printf("-1\n");
						return 0;
					}
				}
			}
		}
	}
	for(int j=0;j<n;j++){
		if(t[j]==0){
			for(int i=0;i<n;i++){
				res[i][j]|=v[j];
				if(s[i]==1){
					if((u[i]|res[i][j])!=u[i]){
						printf("-1\n");
						return 0;
					}
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		if(s[i]==0){
			ll p=res[i][0];
			for(int j=0;j<n;j++){
				p&=res[i][j];
			}
			if(p!=u[i]){
				printf("-1\n");
				return 0;
			}
		}
	}
	for(int j=0;j<n;j++){
		if(t[j]==0){
			ll p=res[0][j];
			for(int i=0;i<n;i++){
				p&=res[i][j];
			}
			if(p!=v[j]){
				printf("-1\n");
				return 0;
			}
		}
	}
	for(int i=0;i<n;i++){
		if(s[i]==1){
			ll p=0;
			for(int j=0;j<n;j++){
				p|=res[i][j];
			}
			if((p|u[i])!=u[i]){
				printf("-1\n");
				return 0;
			}
			vector<int> need;
			for(int l=0;l<64;l++){
				if(((1LL<<l)&u[i])){
					need.push_back(l);
				}
			}
			while(need.size()){
				if(p&(1LL<<need.back())){
					need.pop_back();
					continue;
				}
				bool ok=0;
				for(int j=0;j<n;j++){
					if(t[j]==1&&(v[j]&(1LL<<need.back()))){
						ok=1;
						res[i][j]|=(1LL<<need.back());
						break;
					}
					else if(t[j]==0){
						int cnt=0;
						for(int ii=0;ii<n;ii++){
							if(res[ii][j]&(1LL<<need.back()))cnt++;
						}
						if(cnt<=n-2){
							ok=1;
							res[i][j]|=(1LL<<need.back());
							break;
						}
					}
				}
				if(!ok){
					printf("-1\n");
					return 0;
				}
				else{
					need.pop_back();
				}
			}
		}
	}
	for(int j=0;j<n;j++){
		if(t[j]==1){
			ll p=0;
			for(int i=0;i<n;i++){
				p|=res[i][j];
			}
			if((p|v[j])!=v[j]){
				printf("-1\n");
				return 0;
			}
			vector<int> need;
			for(int l=0;l<64;l++){
				if(((1LL<<l)&v[j])){
					need.push_back(l);
				}
			}
			while(need.size()){
				if(p&(1LL<<need.back())){
					need.pop_back();
					continue;
				}
				bool ok=0;
				for(int i=0;i<n;i++){
					if(s[i]==1&&(u[i]&(1LL<<need.back()))){
						ok=1;
						res[i][j]|=(1LL<<need.back());
						break;
					}
					else if(s[i]==0){
						int cnt=0;
						for(int jj=0;jj<n;jj++){
							if(res[i][jj]&(1LL<<need.back()))cnt++;
						}
						if(cnt<=n-2){
							ok=1;
							res[i][j]|=(1LL<<need.back());
							break;
						}
					}
				}
				if(!ok){
					printf("-1\n");
					return 0;
				}
				else{
					need.pop_back();
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cout << res[i][j] << " ";
		}
		cout << endl;
	}
}