#include <stdio.h>

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, flag[2][501], check[2], count[2];
	unsigned long long ans[501][501] = {}, b = 1;
	for (k = 0; k < 64; k++, b <<= 1) {
		for (i = 1; i <= N; i++) {
			if (S[i] == 0 && (U[i] & 1) == 1) flag[0][i] = 1;
			else if (S[i] == 1 && (U[i] & 1) == 0) flag[0][i] = 0;
			else flag[0][i] = -1;
			if (T[i] == 0 && (V[i] & 1) == 1) flag[1][i] = 1;
			else if (T[i] == 1 && (V[i] & 1) == 0) flag[1][i] = 0;
			else flag[1][i] = -1;
		}
		for (i = 1, check[0] = 0, check[1] = 0; 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++) ans[i][i] += b;
		} else if (check[0] == 0 && check[1] == 3) {
			for (j = 1; j <= N; j++) {
				if (flag[1][j] == 1 || (T[j] == 1 && (V[j] & 1) == 1)) for (i = 1; i <= N; i++) ans[i][j] += b;
			}
		} else if (check[0] == 3 && check[1] == 0) {
			for (i = 1; i <= N; i++) {
				if (flag[0][i] == 1 || (S[i] == 1 && (U[i] & 1) == 1)) for (j = 1; j <= N; j++) ans[i][j] += b;
			}
		} else if ((check[0] | check[1]) == 2) {
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 0) count[0]++;
				else if (S[i] == 1 && (U[i] & 1) == 1) flag[0][i] = 1;
				if (flag[1][i] == 0) count[1]++;
				else if (T[i] == 1 && (V[i] & 1) == 1) flag[1][i] = 1;
			}
			if (count[0] == N) {
				for (i = 1; i <= N; i++) if (flag[1][i] == 1) break;
				if (i <= N) break;
			} else if (count[1] == N) {
				for (i = 1; i <= N; i++) if (flag[0][i] == 1) break;
				if (i <= N) break;
			} 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) break;
			} else if (count[1] == N - 1 && count[0] == 0) {
				for (i = 1; i <= N; i++) if (flag[1][i] == 1 || flag[0][i] == -1) break;
				if (i > N) break;
			}
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 1) count[0]++;
				if (flag[1][i] == 1) count[1]++;
			}
			if (count[0] + count[1] > 0) {
				for (i = 1, j = 1, check[0] = 0, check[1] = 0; check[0] * check[1] == 0; ) {
					for (; (check[0] == 0 && flag[0][i] != 1) || (check[0] == 1 && flag[0][i] == 0); i++) {
						if (i == N) {
							check[0] = 1;
							if (check[1] == 1) break;
							i = 0;
						}
					}
					for (; (check[1] == 0 && flag[1][j] != 1) || (check[1] == 1 && flag[1][j] == 0); j++) {
						if (j == N) {
							check[1] = 1;
							if (check[0] == 1) break;
							j = 0;
						}
					}
					ans[i++][j++] += b;
					if (i > N) {
						check[0] = 1;
						i = 1;
					}
					if (j > N) {
						check[1] = 1;
						j = 1;
					}
				}
			}
		} else if ((check[0] | check[1]) == 1) {
			for (i = 1; i <= N; i++) {
				for (j = 1; j <= N; j++) ans[i][j] += b;
			}
			
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 1) count[0]++;
				else if (S[i] == 0 && (U[i] & 1) == 0) flag[0][i] = 0;
				if (flag[1][i] == 1) count[1]++;
				else if (T[i] == 0 && (V[i] & 1) == 0) flag[1][i] = 0;
			}
			if (count[0] == N) {
				for (i = 1; i <= N; i++) if (flag[1][i] == 0) break;
				if (i <= N) break;
			} else if (count[1] == N) {
				for (i = 1; i <= N; i++) if (flag[0][i] == 0) break;
				if (i <= N) break;
			} 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) break;
			} else if (count[1] == N - 1 && count[0] == 0) {
				for (i = 1; i <= N; i++) if (flag[1][i] == 0 || flag[0][i] == -1) break;
				if (i > N) break;
			}
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 0) count[0]++;
				if (flag[1][i] == 0) count[1]++;
			}
			if (count[0] + count[1] > 0) {
				for (i = 1, j = 1, check[0] = 0, check[1] = 0; check[0] * check[1] == 0; ) {
					for (; (check[0] == 0 && flag[0][i] != 0) || (check[0] == 1 && flag[0][i] == 1); i++) {
						if (i == N) {
							check[0] = 1;
							if (check[1] == 1) break;
							i = 0;
						}
					}
					for (; (check[1] == 0 && flag[1][j] != 0) || (check[1] == 1 && flag[1][j] == 1); j++) {
						if (j == N) {
							check[1] = 1;
							if (check[0] == 1) break;
							j = 0;
						}
					}
					ans[i++][j++] -= b;
					if (i > N) {
						check[0] = 1;
						i = 1;
					}
					if (j > N) {
						check[1] = 1;
						j = 1;
					}
				}
			}
		} else break;
		
		for (i = 1; i <= N; i++) {
			U[i] >>= 1;
			V[i] >>= 1;
		}
	}

	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;
}