#include <stdio.h>

int consistent_matrix(int N, int S[], int T[], int u[], int v[], int A[][501])
{
	int i, j, k, flag[2][501], check[2] = {}, count[2] = {};
	for (i = 1; i <= N; i++) {
		for (j = 1; j <= N; j++) A[i][j] = 0;
	}
	
	for (i = 1; i <= N; i++) {
		if (S[i] == 0 && u[i] == 1) flag[0][i] = 1;
		else if (S[i] == 1 && u[i] == 0) flag[0][i] = 0;
		else flag[0][i] = -1;
		if (T[i] == 0 && v[i] == 1) flag[1][i] = 1;
		else if (T[i] == 1 && v[i] == 0) flag[1][i] = 0;
		else flag[1][i] = -1;
	}
	for (i = 1; i <= N; i++) {
		if (flag[0][i] == 1) check[0] |= 1;
		else if (flag[0][i] == 0) check[0] |= 2;
		if (flag[1][i] == 1) check[1] |= 1;
		else if (flag[1][i] == 0) check[1] |= 2;
	}
		
	if (check[0] == 0 && check[1] == 0) {
		for (i = 1; i <= N; i++) A[i][i] = 1;
	} else if (check[0] == 0 && check[1] == 3) {
		for (j = 1; j <= N; j++) {
			if (v[j] == 1) for (i = 1; i <= N; i++) A[i][j] = 1;
		}
	} else if (check[0] == 3 && check[1] == 0) {
		for (i = 1; i <= N; i++) {
			if (u[i] == 1) for (j = 1; j <= N; j++) A[i][j] = 1;
		}
	} else if ((check[0] | check[1]) == 2) {
		for (i = 1; i <= N; i++) {
			if (flag[0][i] == 0) count[0]++;
			else if (u[i] == 1) flag[0][i] = 1;
			if (flag[1][i] == 0) count[1]++;
			else if (v[i] == 1) flag[1][i] = 1;
		}
		if (count[0] == N) {
			for (i = 1; i <= N; i++) if (flag[1][i] == 1) return -1;
		} else if (count[1] == N) {
			for (i = 1; i <= N; i++) if (flag[0][i] == 1) return -1;
		} else if (count[0] == N - 1 && count[1] == 0) {
			for (i = 1; i <= N; i++) if (flag[0][i] == 1 || flag[1][i] == -1) break;
			if (i > N) return -1;
			else if (flag[0][i] == 1) {
				for (j = 1; j <= N; j++) A[i][j] = 1;
			} else {
				for (k = i, i = 1; flag[0][i] == 0; i++);
				for (j = 1; j <= N; j++) {
					if (j == k) continue;
					else A[i][j] = 1;
				}
			}
		} else if (count[1] == N - 1 && count[0] == 0) {
			for (i = 1; i <= N; i++) if (flag[0][i] == -1 || flag[1][i] == 1) break;
			if (i > N) return -1;
			else if (flag[1][i] == 1) {
				for (j = 1; j <= N; j++) A[j][i] = 1;
			} else {
				for (k = i, i = 1; flag[1][i] == 0; i++);
				for (j = 1; j <= N; j++) {
					if (j == k) continue;
					else A[j][i] = 1;
				}
			}
		} else {
			for (i = 1; i <= N; i++) {
				if (flag[0][i] == 0) continue;
				for (j = 1; j <= N; j++) {
					if (flag[1][j] != 0) A[i][j] = 1;
				}
			}
			if (count[0] == 0) {
				for (j = 1, i = 1; j <= N; j++) {
					if (flag[1][j] == 0) continue;
					A[i++][j] = 0;
				}
			} else if (count[1] == 0) {
				for (i = 1, j = 1; i <= N; i++) {
					if (flag[0][i] == 0) continue;
					A[i][j++] = 0;
				}
			}
		}
	} else if ((check[0] | check[1]) == 1) {
		for (i = 1; i <= N; i++) {
			for (j = 1; j <= N; j++) A[i][j] = 1;
		}
		for (i = 1; i <= N; i++) {
			if (flag[0][i] == 1) count[0]++;
			else if (u[i] == 0) flag[0][i] = 0;
			if (flag[1][i] == 1) count[1]++;
			else if (v[i] == 0) flag[1][i] = 0;
		}
		if (count[0] == N) {
			for (i = 1; i <= N; i++) if (flag[1][i] == 0) return -1;
		} else if (count[1] == N) {
			for (i = 1; i <= N; i++) if (flag[0][i] == 0) return -1;
		} else if (count[0] == N - 1 && count[1] == 0) {
			for (i = 1; i <= N; i++) if (flag[0][i] == 0 || flag[1][i] == -1) break;
			if (i > N) return -1;
			else if (flag[0][i] == 0) {
				for (j = 1; j <= N; j++) A[i][j] = 0;
			} else {
				for (k = i, i = 1; flag[0][i] == 1; i++);
				for (j = 1; j <= N; j++) {
					if (j == k) continue;
					else A[i][j] = 0;
				}
			}
		} else if (count[1] == N - 1 && count[0] == 0) {
			for (i = 1; i <= N; i++) if (flag[0][i] == -1 || flag[1][i] == 0) break;
			if (i > N) return -1;
			else if (flag[1][i] == 0) {
				for (j = 1; j <= N; j++) A[j][i] = 0;
			} else {
				for (k = i, i = 1; flag[1][i] == 1; i++);
				for (j = 1; j <= N; j++) {
					if (j == k) continue;
					else A[j][i] = 0;
				}
			}
		} else {
			for (i = 1; i <= N; i++) {
				if (flag[0][i] == 1) continue;
				for (j = 1; j <= N; j++) {
					if (flag[1][j] != 1) A[i][j] = 0;
				}
			}
			if (count[0] == 0) {
				for (j = 1, i = 1; j <= N; j++) {
					if (flag[1][j] == 1) continue;
					A[i++][j] = 1;
				}
			} else if (count[1] == 0) {
				for (i = 1, j = 1; i <= N; i++) {
					if (flag[0][i] == 1) continue;
					A[i][j++] = 1;
				}
			}
		}
	} else return -1;

	return 0;
}

int main()
{
	int i, N, S[501], T[501];
	unsigned long long U[501], V[501];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(S[i]));
	for (i = 1; i <= N; i++) scanf("%d", &(T[i]));
	for (i = 1; i <= N; i++) scanf("%llu", &(U[i]));
	for (i = 1; i <= N; i++) scanf("%llu", &(V[i]));
	if (N == 1) {
		if (U[1] == V[1]) printf("%llu\n", U[1]);
		else printf("-1\n");
		fflush(stdout);
		return 0;
	}
	
	int j, k, u[501], v[501], A[501][501];
	unsigned long long ans[501][501] = {}, b = 1;
	for (k = 0; k < 64; k++, b <<= 1) {
		for (i = 1; i <= 500; i++) {
			u[i] = U[i] & 1;
			v[i] = V[i] & 1;
		}
		for (i = 1; i <= N; i++) {
			U[i] >>= 1;
			V[i] >>= 1;
		}
		if (consistent_matrix(N, S, T, u, v, A) == -1) break;
		for (i = 1; i <= N; i++) {
			for (j = 1; j <= N; j++) ans[i][j] += b * A[i][j];
		}
	}

	if (k == 64) {
		for (i = 1; i <= N; i++) {
			for (j = 1; j < N; j++) printf("%llu ", ans[i][j]);
			printf("%llu\n", ans[i][N]);
		}
	} else printf("-1\n");
	fflush(stdout);
	return 0;
}

Measured memory_bytes: 8392