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>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define iFOR(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())

using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long UL; // ULLONG_MAX, %llu
typedef unsigned int UI; //UINT_MAX %u
typedef double DD;
typedef long double LD;
typedef pair<LL,LL> PLL;
typedef pair<DD,DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;

void termi(){
    printf("-1");
    exit(0);
}
const int N = 505;
bool lockrow[64][N], lockcol[64][N];
UL ans[N][N], tmp[64][N][N];
int cmd[N][2], cnt_row[64][N], cnt_col[64][N];
UL val[N][2];
void eval(int k, int i, int j, int ch){
    cnt_row[k][i]+=ch; cnt_col[k][j]+=ch;
}
int main(){
    int n;
    scanf("%d",&n);
    FOR(i,0,1){
        FOR(j,1,n){
            scanf(" %d",&cmd[j][i]);
        }
    }
    FOR(i,0,1){
        FOR(j,1,n){
            scanf(" %llu",&val[j][i]);
        }
    }
    memset(tmp, -1, sizeof tmp); // undetermine
    // Fundamental determination
    int bit_max = 63;
    FOR(k, 0, bit_max){
        FOR(i, 1, n){
            int a = ((val[i][0]>>k)&1);
            if(cmd[i][0] != a){
                lockrow[k][i] = true;
                FOR(j, 1, n){
                    if(tmp[k][i][j] == -1) tmp[k][i][j] = a;
                    else if(tmp[k][i][j] != a) termi();
                    if(!a) eval(k,i,j,1);
                }
            }
            int b = ((val[i][1]>>k)&1);
            if(cmd[i][1] != b){
                lockcol[k][i] = true;
                FOR(j, 1, n){
                    if(tmp[k][j][i] == -1) tmp[k][j][i] = b;
                    else if(tmp[k][j][i] != b) termi();
                    if(!b) eval(k,j,i,1);
                }
            }
        }
        FOR(i, 1, n){
            FOR(j, 1, n){
                if(lockrow[k][i] || lockcol[k][j]) continue;
                if(cmd[i][0] == cmd[j][1]) tmp[k][i][j] = cmd[i][0];
                else tmp[k][i][j] = 0;
                if(!tmp[k][i][j]) eval(k, i, j, 1);
            }
        }
        FOR(i, 1, n){
            if(lockrow[k][i] || cmd[i][0] == 0) continue;
            int ch = 0;
            FOR(j, 1, n){
                if(tmp[k][i][j]) ch = 1;
            }
            if(ch) continue;
            FOR(j, 1, n){
                if(lockcol[k][j] || cnt_col[k][j] == 1) continue;
                tmp[k][i][j] = 1; eval(k, i, j, -1);
                break;
            }
        }
        FOR(j, 1, n){
            if(lockcol[k][j] || cmd[j][1] == 0) continue;
            int ch = 0;
            FOR(i, 1, n){
                if(tmp[k][i][j]) ch = 1;
            }
            if(ch) continue;
            FOR(i, 1, n){
                if(lockrow[k][i] || cnt_row[k][i] == 1) continue;
                tmp[k][i][j] = 1; eval(k, i, j, -1);
                break;
            }
        }
        FOR(i, 1, n){
            FOR(j, 1, n){
                ans[i][j] |= ((UL)tmp[k][i][j]<<k);
            }
        }
    }
    FOR(i, 1, n){
        UL res[2];
        res[0] = ULLONG_MAX; res[1] = 0;
        FOR(j, 1, n) res[0] &= ans[i][j], res[1] |= ans[i][j];
        if(res[cmd[i][0]] != val[i][0]) termi();
        res[0] = ULLONG_MAX, res[1] = 0;
        FOR(j, 1, n) res[0] &= ans[j][i], res[1] |= ans[j][i];
        if(res[cmd[i][1]] != val[i][1]) termi();
    }
    FOR(i, 1, n){
        FOR(j, 1, n){
            printf("%llu ",ans[i][j]);
        }
        printf("\n");
    }
	return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */