class UnionFind: def __init__(self, n): self.par = [-1]*n self.rank = [0]*n def Find(self, x): if self.par[x] < 0: return x else: self.par[x] = self.Find(self.par[x]) return self.par[x] def Unite(self, x, y): x = self.Find(x) y = self.Find(y) if x != y: if self.rank[x] < self.rank[y]: self.par[y] += self.par[x] self.par[x] = y else: self.par[x] += self.par[y] self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 def Same(self, x, y): return self.Find(x) == self.Find(y) def Size(self, x): return -self.par[self.Find(x)] import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import defaultdict t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) uf = UnionFind(n) for k in range(2, n+1): j = 2*k while j < n+1: uf.Unite(k-1, j-1) j += k DA = defaultdict(lambda: []) for i, a in enumerate(A): DA[uf.Find(i)].append(a) DB = defaultdict(lambda: []) for i, b in enumerate(B): DB[uf.Find(i)].append(b) flag = True for k in DA.keys(): if sorted(DA[k]) != sorted(DB[k]): flag = False break if flag: print('Yes') else: print('No')