import sys from collections import Counter input = lambda: sys.stdin.readline().rstrip() INF = 10 ** 19 from math import gcd class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 def solve(): n = int(input()) a = [0] + list(map(int,input().split())) b = [0] + list(map(int,input().split())) if a[1] != b[1]: return 'No' now = 1 ans = 'Yes' U = dsu(n + 1) for i in range(2,n + 1): for j in range(2, n + 1): if i * j // gcd(i, j) <= n: U.merge(i, j) break a_now = [[] for _ in range(n + 1)] b_now = [[] for _ in range(n + 1)] for i in range(2, n + 1): a_now[U.leader(i)].append(a[i]) b_now[U.leader(i)].append(b[i]) for i in range(n): a_now[i].sort() b_now[i].sort() if a_now[i] != b_now[i]: ans = 'No' break return ans t = int(input()) for _ in range(t): print(solve())