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())) if A[0]!=B[0]: print("No") continue dict_A=defaultdict(int) dict_B=defaultdict(int) ans="Yes" check=[False]*(N+1) for i in range(2,N+1): if check[i]: continue if 2*i>N: if A[i-1]!=B[i-1]: ans="No" continue for j in range(i,N+1,i): if check[j]: continue check[j]=True dict_A[A[j-1]]+=1 dict_B[B[j-1]]+=1 for k in dict_A.keys(): if dict_A[k]!=dict_B[k]: ans="No" break print(ans)