#pragma warning(disable:4996) #include #include #include #include #include #include using namespace std; const int maxn = 100005; int a[maxn],b[maxn]; bool v[maxn]; void dfs(int x) { v[x] = 1; if (!v[a[x]]) dfs(a[x]); } int main() { int t,n,m; cin >> t; while (t--) { cin >> n; memset(v, 0, sizeof(v)); for (int i = 0;i < n;i++) { scanf("%d", &b[i]); } for (int i = 0;i < n;i++) scanf("%d", &a[b[i]]); int ret = n; for (int i = 1;i <= n;i++) if (a[i] != i&&!v[i]) { dfs(i); ret--; } cout << ret << endl; } return 0; }