v = [[] for i in range(0, 100005)]
w = [[] for i in range(0, 100005)]
def dfs(a, b):
if len(v[a]) == 0 and len(w[b]) == 0:
return True
elif len(v[a]) != len(w[b]):
return False
elif len(v[a]) == 1:
return dfs(v[a][0], w[b][0])
else:
return (dfs(v[a][0], w[b][0]) and dfs(v[a][1], w[b][1])) or (dfs(v[a][0], w[b][1]) and dfs(v[a][1], w[b][0]))
t = int(input())
for z in range(0, t):
n = int(input())
for i in range(0, 100005):
v[i].clear()
w[i].clear()
A = list(map(int, input().split()))
for i in range(0, n):
v[A[i]].append(i + 1)
B = list(map(int, input().split()))
for j in range(0, n):
w[B[j]].append(j + 1)
print(int(dfs(0, 0)))
diA9IFtbXSBmb3IgaSBpbiByYW5nZSgwLCAxMDAwMDUpXQp3ID0gW1tdIGZvciBpIGluIHJhbmdlKDAsIDEwMDAwNSldCgoKZGVmIGRmcyhhLCBiKToKICAgIGlmIGxlbih2W2FdKSA9PSAwIGFuZCBsZW4od1tiXSkgPT0gMDoKICAgICAgICByZXR1cm4gVHJ1ZQogICAgZWxpZiBsZW4odlthXSkgIT0gbGVuKHdbYl0pOgogICAgICAgIHJldHVybiBGYWxzZQogICAgZWxpZiBsZW4odlthXSkgPT0gMToKICAgICAgICByZXR1cm4gZGZzKHZbYV1bMF0sIHdbYl1bMF0pCiAgICBlbHNlOgogICAgICAgIHJldHVybiAoZGZzKHZbYV1bMF0sIHdbYl1bMF0pIGFuZCBkZnModlthXVsxXSwgd1tiXVsxXSkpIG9yIChkZnModlthXVswXSwgd1tiXVsxXSkgYW5kIGRmcyh2W2FdWzFdLCB3W2JdWzBdKSkKCgp0ID0gaW50KGlucHV0KCkpCmZvciB6IGluIHJhbmdlKDAsIHQpOgogICAgbiA9IGludChpbnB1dCgpKQogICAgZm9yIGkgaW4gcmFuZ2UoMCwgMTAwMDA1KToKICAgICAgICB2W2ldLmNsZWFyKCkKICAgICAgICB3W2ldLmNsZWFyKCkKICAgIEEgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpCiAgICBmb3IgaSBpbiByYW5nZSgwLCBuKToKICAgICAgICB2W0FbaV1dLmFwcGVuZChpICsgMSkKICAgIEIgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpCiAgICBmb3IgaiBpbiByYW5nZSgwLCBuKToKICAgICAgICB3W0Jbal1dLmFwcGVuZChqICsgMSkKICAgIHByaW50KGludChkZnMoMCwgMCkpKQo=