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;

int a[64][500][500];
uint64_t ans[500][500];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> s(n), t(n); 
    vector<uint64_t> u(n), v(n);
    uint64_t o = 1;
    for (auto &x: s) cin >> x;
    for (auto &x: t) cin >> x;
    for (auto &x: u) cin >> x;
    for (auto &x: v) cin >> x;
    bool ok = true;
    for (int k = 0; k < 64; k++) {
        vector<int> sv, tv;
        for (int i = 0; i < n; i++) {
            bool p = u[i] & (o << k);
            bool q = v[i] & (o << k);
            if (s[i] ^ p) {
                for (int j = 0; j < n; j++) {
                    if (a[k][i][j] == s[i] + 1) ok = false;
                    a[k][i][j] = 2 - s[i];
                }
            }
            if (t[i] ^ q) {
                for (int j = 0; j < n; j++) {
                    if (a[k][j][i] == t[i] + 1) ok = false;
                    a[k][j][i] = 2 - t[i];
                }
            }            
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                bool p = u[i] & (o << k);
                bool q = v[j] & (o << k);
                if (p == q) a[k][i][j] = p + 1;
            }
        }
        vector<int> rc(n, 0), cc(n, 0);
        for (int i = 0; i < n; i++) {       
            bool p = u[i] & (o << k);
            bool q = v[i] & (o << k);
            int c = 0, d = 0;
            for (int j = 0; j < n; j++) {
                c |= 1 << a[k][i][j];
                d |= 1 << a[k][j][i];
                if (a[k][i][j] == 0) rc[i]++, cc[j]++;
            }
            if (s[i] && p && !(c&4)) sv.emplace_back(i);
            if (t[i] && q && !(d&4)) tv.emplace_back(i);
            if (!s[i] && !p && !(c&3)) ok = false;
            if (!t[i] && !q && !(d&3)) ok = false;
            if (!s[i] && !p && !(c&2)) rc[i]--;
            if (!t[i] && !q && !(d&2)) cc[i]--;
        }
        for (auto &i: sv) {
            for (int j = 0; j < n; j++) {
                if (a[k][i][j] == 0 && cc[j] > 0) {
                    cc[j]--; a[k][i][j] = 2;
                    break;
                }
                if (j == n-1) ok = false; 
            }
        }
        for (auto &j: tv) {
            for (int i = 0; i < n; i++) {
                if (a[k][i][j] == 0 && rc[i] > 0) {
                    rc[i]--; a[k][i][j] = 2;
                    break;
                }
                if (i == n-1) ok = false; 
            }
        }
    }
    for (int k = 0; k < 64; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (a[k][i][j] == 2) ans[i][j] |= o << k;
            }
        }
    }
    if (!ok) {
        cout << -1 << "\n";
        return 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (j) cout << " ";
            cout << ans[i][j];
        }
        cout << "\n";
    }
    for (int i = 0; i < n; i++) {
        uint64_t q = ans[i][0];
        for (int j = 0; j < n; j++) {
            if (s[i]) q |= ans[i][j];
            else q &= ans[i][j];
        }
        assert(q == u[i]);
    }
    for (int i = 0; i < n; i++) {
        uint64_t q = ans[0][i];
        for (int j = 0; j < n; j++) {
            if (t[i]) q |= ans[j][i];
            else q &= ans[j][i];
        }
        assert(q == v[i]);
    }
    return 0;
}