#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) { int N; cin >> N; vectorA(N),B(N),cnt(N); for(int i = 0; i < N; i++) { cin >> A[i]; A[i]--; cnt[A[i]]++; } for(int i = 0; i < N; i++) { cin >> B[i]; B[i]--; } vectorused(N,-1); queueque; for(int i = 0; i < N; i++) { if(cnt[i] == 0) { que.push(i); used[i] = 0; } } while(!que.empty()) { int x = que.front(); que.pop(); cnt[A[x]]--; if(cnt[A[x]] == 0) { used[A[x]] = 0; que.push(x); } } bool flag = true; for(int i = 0; i < N; i++) { vectorf(N); f[A[i]] = 1; int now = A[i]; while(true) { now = A[now]; if(f[now]) break; f[now] = 1; } if(!f[B[i]]) { flag = false; } } if(!flag) { cout << "No" << "\n"; continue; } vectorok(N),nxt(N,-1),pre(N,-1); for(int i = 0; i < N; i++) { if(used[i] == -1) { ok[B[i]]++; nxt[i] = A[i]; pre[A[i]] = i; } } for(int i = 0; i < N; i++) { if(ok[i] == 0 && nxt[i] != -1) { que.push(i); } } while(!que.empty()) { int x = que.front(); que.pop(); nxt[pre[x]] = nxt[x]; pre[nxt[x]] = pre[x]; ok[B[x]]--; if(ok[B[x]] == 0 && nxt[B[x]] != -1) { que.push(B[x]); } used[x] = 0; } for(int i = 0; i < N; i++) { if(used[i] == -1 && nxt[i] != B[i]) { flag = false; } } cout << ((flag)?"Yes":"No") << "\n"; } }