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 h, w;
vector< int > opy, opx;
vector< uint64_t > y, x, by, bx;
vector< vector< uint64_t > > ans, b;

bool solvebit() {
  for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) b[i][j] = -1;
  vector< int > ry, rx;
  for(int i = 0; i < h; i++) {
    if(opy[i] == by[i]) for(int j = 0; j < w; j++) b[i][j] = by[i];
    else ry.push_back(i);
  }
  for(int i = 0; i < w; i++) {
    if(opx[i] == bx[i]) {
      for(int j = 0; j < h; j++) {
        if(b[j][i] == !bx[i]) return false;
        b[j][i] = bx[i];
      }
    } else rx.push_back(i);
  }
  int rh = ry.size(), rw = rx.size();
  if(min(rh, rw) >= 2) {
    if(rw <= rh) {
      for(int i = 0; i < rh; i++) {
        b[ry[i]][rx[i % rw]] = 1;
        b[ry[i]][rx[(i + 1) % rw]] = 0;
      }
    } else {
      for(int i = 0; i < rw; i++) {
        b[ry[i % rh]][rx[i]] = 1;
        b[ry[(i + 1) % rh]][rx[i]] = 0;
      }
    }
  } else if(min(rh, rw) == 1) {
    if(rw == 1) {
      for(int i = 0; i < rh; i++) {
        bool g = false;
        for(int j = 0; j < w; j++) if(b[ry[i]][j] == by[ry[i]]) g = true;
        if(!g) b[ry[i]][rx[0]] = by[ry[i]];
      }
      bool g = false;
      for(int i = 0; i < h; i++) {
        if(b[i][rx[0]] == bx[rx[0]]) {
          g = true;
          break;
        }
        if(b[i][rx[0]] == -1) {
          b[i][rx[0]] = bx[rx[0]];
          g = true;
          break;
        }
      }
      if(!g) return false;
    } else {
      for(int i = 0; i < rw; i++) {
        bool g = false;
        for(int j = 0; j < h; j++) if(b[j][rx[i]] == bx[rx[i]]) g = true;
        if(!g) b[ry[0]][rx[i]] = bx[rx[i]];
      }
      bool g = false;
      for(int i = 0; i < w; i++) {
        if(b[ry[0]][i] == by[ry[0]]) {
          g = true;
          break;
        }
        if(b[ry[0]][i] == -1) {
          b[ry[0]][i] = by[ry[0]];
          g = true;
          break;
        }
      }
      if(!g) return false;
    }
  } else if(min(rh, rw) == 0) {
    for(int i = 0; i < h; i++) {
      int beetand = b[i][0], beetor = b[i][0];
      for(int j = 1; j < w; j++) {
        beetand &= b[i][j];
        beetor |= b[i][j];
      }
      if((opy[i] && beetand != by[i]) || (!opy[i] && beetor != by[i])) return false;
    }
    for(int i = 0; i < w; i++) {
      int beetand = b[0][i], beetor = b[0][i];
      for(int j = 1; j < h; j++) {
        beetand &= b[j][i];
        beetor |= b[j][i];
      }
      if((opx[i] && beetand != bx[i]) || (!opx[i] && beetor != bx[i])) return false;
    }
  }
  for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(b[i][j] == -1) b[i][j] = 0;
  return true;
}

int main() {
  string op;
  int n;
  cin >> n;
  h = n, w = n;
  opy.resize(h);
  opx.resize(w);
  y.resize(h);
  by.resize(h);
  x.resize(w);
  bx.resize(w);
  ans.resize(h, vector< uint64_t >(w));
  b.resize(h, vector< uint64_t >(w));
  for(int i = 0; i < h; i++) {
    cin >> opy[i];
    opy[i] ^= 1;
  }
  for(int i = 0; i < w; i++) {
    cin >> opx[i];
    opx[i] ^= 1;
  }
  for(int i = 0; i < h; i++) {
    cin >> y[i];
  }
  for(int i = 0; i < w; i++) {
    cin >> x[i];
  }
  bool f = true;
  for(int k = 0; k < 64; k++) {
    for(int i = 0; i < h; i++) by[i] = (y[i] & (((uint64_t) 1) << k)) != 0;
    for(int i = 0; i < w; i++) bx[i] = (x[i] & (((uint64_t) 1) << k)) != 0;
    if(!solvebit()) f = false;
    for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) ans[i][j] += b[i][j] * (((uint64_t) 1) << k);
  }
  if(!f) {
    cout << -1 << endl;
    return 0;
  }
  for(int i = 0; i < h; i++) {
    for(int j = 0; j < w; j++) {
      if(j) cout << " ";
      cout << ans[i][j];
    }
    cout << endl;
  }
}