# https://github.com/tyuyu-62/cp-library from collections import deque, defaultdict from itertools import permutations, product from bisect import bisect_left, bisect_right from heapq import heappush, heappop from random import randint, shuffle from time import perf_counter as pc def II(): return int(input()) def LI(dec=0): return [int(x) - dec for x in input().split()] def SI(): return input() def LS(): return list(input().split()) mod = 998244353 inf = 2002002002002002002 import sys sys.setrecursionlimit(10 ** 6) input = lambda: sys.stdin.readline().rstrip() _buf = [] def print(*args, sep=" "): _buf.append(sep.join(map(str, args))) def debug(*args): if DEBUG: sys.stdout.write(" ".join(map(str, args)) + "\n") DEBUG = True def solve(): N = II() R = LI() C = LI() if N == 1: if R != C: print(-1) else: print(*R) elif N == 2: if R == C and R[0] == R[1]: print(*R) print(*R) else: print(-1) else: A = [[R[i]] * N for i in range(N)] change = [0] * N if len(set(C)) == 1 and N <= 4: print(-1) return li = list(range(N)) if N == 3: if len(set(C)) == 3: for j in range(N): if C[j] != R[-1]: li = list(range(j + 1, N)) + list(range(j + 1)) for j in li: for i in range(N): if R[i] == C[j]: continue if (change[i] + 1) * 2 >= N: continue change[i] += 1 A[i][j] = C[j] break else: x = sorted(C)[1] y = 0 for c in C: y ^= c for j in range(N): for i in range(N): if R[i] == x and C[j] == y: A[i][j] = C[j] if R[i] != x and C[j] == x and change[i] == 0: A[i][j] = C[j] change[i] = 1 break elif N == 4: x = sorted(C)[1] if C.count(x) == 3: y = x for c in C: y ^= c for j in range(N): for i in range(N): if R[i] == x and C[j] == y: A[i][j] = C[j] if R[i] != x and C[j] == x and change[i] == 0: A[i][j] = C[j] change[i] = 1 break else: for j in li: for i in range(N): if R[i] == C[j]: continue now = A[i].count(R[i]) for k in range(1, N + 1): if k == R[i]: continue if A[i].count(k) >= now - 1: break else: if A[i].count(C[j]) + 1 >= now - 1: continue A[i][j] = C[j] break else: for j in li: for i in range(N): if R[i] == C[j]: continue if (change[i] + 1) * 2 >= N: continue change[i] += 1 A[i][j] = C[j] break if 0: for i in range(N): cnt = [0] * (N + 1) for j in range(N): cnt[A[i][j]] += 1 mx = max(cnt) assert cnt.count(mx) == 1, A assert cnt.index(mx) == R[i], A for j in range(N): cnt = [0] * (N + 1) for i in range(N): cnt[A[i][j]] += 1 mx = max(cnt) assert cnt.count(mx) == 1, A assert cnt.index(mx) == C[j], A for i in range(N): print(*A[i]) return if __name__ == "__main__": T = 1 T = II() for _ in range(T): solve() sys.stdout.write("\n".join(_buf) + "\n")