import sys readline = sys.stdin.readline from collections import * class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] T = int(readline()) for _ in range(T): N = int(readline()) A = list(map(int, readline().split())) B = list(map(int, readline().split())) U = UnionFind(N+1) for i in range(2, N+1): for j in range(2 * i, N+1, i): U.union(i, j) D1 = [defaultdict(int) for i in range(N+1)] D2 = [defaultdict(int) for i in range(N+1)] for i in range(1, N + 1): D1[U.find(i)][A[i-1]] += 1 D2[U.find(i)][B[i-1]] += 1 for i in range(N+1): if D1[i] != D2[i]: print("No") break else: print("Yes")