#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 500, lgsz = 63, ndsz = 2 * nsz + 5, egsz = 2 * ndsz + ndsz * ndsz;
const int inf = 0x3f3f3f3f;
int n, rt[nsz], ct[nsz + 5], rb[nsz + 5], cb[nsz + 5];
unsigned long long ra[nsz + 5], ca[nsz + 5], ans[nsz + 5][nsz + 5];
int sz, src, snk, nd[ndsz + 5][2], fm_r[ndsz + 5];
int d[ndsz + 5], cur[ndsz + 5], flow, cost;

struct graph {
	int sz, hd[ndsz + 5];
	
	struct edge {
		int nxt, to, f, c;
		
		edge(int nxt = 0, int to = 0, int f = 0, int c = 0) {
			this->nxt = nxt, this->to = to, this->f = f, this->c = c;
		}
	};
	edge e[2 * egsz];
	
	inline edge& operator [] (int id) { return e[id]; }
	
	void inline clear(int to) {
		fill(hd, hd + to + 1, 0);
		sz = 0;
	}
	
	void inline link(int u, int v, int f = 1, int c = 0) {
		e[++sz] = edge(hd[u], v, f, +c), hd[u] = sz;
		e[++sz] = edge(hd[v], u, 0, -c), hd[v] = sz;
	}
};
graph g;

int inline rev(int id) { return id & 1 ? ++id : --id; }

int inline nxt(int &id, int mx = ndsz) { id = id == mx ? 0 : id + 1; return id; }

bool inline spfa() {
	static bool vis[ndsz + 5];
	static int q[ndsz + 5];
	int ql = 0, qr = 0;
	cont (u, sz) {
		d[u] = inf;
	}
	d[src] = 0, vis[src] = 1, q[nxt(qr)] = src;
	for (; ql != qr;) {
		int u = q[nxt(ql)];
		vis[u] = 0;
		for (int i = g.hd[u]; i; i = g[i].nxt) {
			int v = g[i].to, f = g[i].f, c = g[i].c;
			if (!f || d[v] <= d[u] + c)  continue;
			d[v] = d[u] + c;
			if (!vis[v])  q[nxt(qr)] = v, vis[v] = 1;
		}
	}
	return d[snk] < inf;
}

int dfs(int u, int &cost, int mf = inf) {
	static int vis[ndsz + 5];
	if (u == snk)  return mf;
	int res = 0;
	vis[u] = 1;
	for (int i = g.hd[u]; i; i = g[i].nxt) {
		int v = g[i].to, f = g[i].f, c = g[i].c;
		if (!f || vis[v] || d[v] != d[u] + c)  continue;
		int cur = dfs(v, cost, min(f, mf - res));
		if (cur) {
			res += cur;
			cost += c * cur;
			g[i].f -= cur, g[rev(i)].f += cur;
			if (res == mf)  break;
		}
	}
	vis[u] = 0;
	return res;
}

void inline mcmf(int &flow, int &cost) {
	for (; spfa(); flow += dfs(src, cost));
}

void inline graph_init(int &sum) {
	g.clear(sz), sz = 0;
	src = ++sz, snk = ++sz;
	cont (u, n)  nd[u][0] = ++sz, fm_r[sz] = u, nd[u][1] = ++sz;
	cont (r, n) cont (c, n) {
		g.link(nd[r][0], nd[c][1], 1, 0);
	}
	cont (r, n) {
		if (rt[r] == 0 && rb[r] == 0)  g.link(src, nd[r][0], n - 1, 0);
		if (rt[r] == 0 && rb[r] == 1)  g.link(src, nd[r][0], n, -1), sum -= n;
		if (rt[r] == 1 && rb[r] == 0)  g.link(src, nd[r][0], 0, 0);
		if (rt[r] == 1 && rb[r] == 1)  g.link(src, nd[r][0], 1, -1), g.link(src, nd[r][0], n - 1, 0), --sum;
	}
	cont (c, n) {
		if (ct[c] == 0 && cb[c] == 0)  g.link(nd[c][1], snk, n - 1, 0);
		if (ct[c] == 0 && cb[c] == 1)  g.link(nd[c][1], snk, n, -1), sum -= n;
		if (ct[c] == 1 && cb[c] == 0)  g.link(nd[c][1], snk, 0, 0);
		if (ct[c] == 1 && cb[c] == 1)  g.link(nd[c][1], snk, 1, -1), g.link(nd[c][1], snk, n - 1, 0), --sum;
	}
}

int main() {
	scanf("%d", &n);
	cont (i, n)  scanf("%d", &rt[i]);
	cont (i, n)  scanf("%d", &ct[i]);
	cont (i, n)  scanf("%llu", &ra[i]);
	cont (i, n)  scanf("%llu", &ca[i]);
	circ (k, 0, 63) {
		cont (i, n)  rb[i] = ra[i] >> (unsigned long long)(k) & 1;
		cont (i, n)  cb[i] = ca[i] >> (unsigned long long)(k) & 1;
		
		int sum = 0, flow = 0, cost = 0;
		graph_init(sum);
		mcmf(flow, cost);
 		if (sum != cost) {
			printf("-1\n");
			return 0;
		}
		cont (c, n) {
			int u = nd[c][1];
			for (int i = g.hd[u]; i; i = g[i].nxt) {
				int v = g[i].to, f = g[i].f;
				if (v == snk)  continue;
				int r = fm_r[v];
				ans[r][c] += (unsigned long long)(f) << (unsigned long long)(k);
			}
		}
	}
	cont (r, n) cont (c, n) {
		printf("%llu%c", ans[r][c], c == n ? '\n' : ' ');
	}
}