N = 100001 fp = [0] * N for i in range(2, len(fp)): if fp[i] == 0: fp[i] = i for j in range(i * i, len(fp), i): if fp[j] == 0: fp[j] = i ncases = int(input()) for caseno in range(ncases): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c1, c2 = {}, {} for i in range(1, len(a)): if a[i] == b[i]: continue if a[i] not in c1: c1[a[i]] = [] if b[i] not in c2: c2[b[i]] = [] c1[a[i]].append(fp[i + 1]) c2[b[i]].append(fp[i + 1]) def check(): if a[0] != b[0]: return False for x in c1: a1 = c1.get(x, []); a2 = c2.get(x, []) if len(a1) != len(a2): return False a1.sort(); a2.sort() if a1[0] * a2[-1] > n and a1[-1] * a2[0] > n: return False return True print("Yes" if check() else "No")