#define PROB Training #include #include #include #include typedef std::pair PII; #define f(x,y,z) for(int x = (y),__ = (z);x < __;++x) #define g(x,y,z) for(int x = (y),__ = (z);x <= __;++x) #define gd(x,y,z) for(int x = (y),__ = (z);x >= __;--x) #define CLR(X) memset(X,0,sizeof(X)) #define SET(ARR,X) memset(ARR,X,sizeof(ARR)) #define R(X,Y) f(X,0,Y) #define G(x,y) g(X,0,Y) #define MAXN 1033 #ifdef WIN32 #define LLD "%I64d" #define LLU "%I64u" #else #define LLD "%lld" #define LLU "%llu" #endif typedef long long LL; typedef long double real; int T; int n; int a[100033],s[100033]; bool vis[100033]; int ans,cnt,tee; int main(){ scanf("%d",&T); while (T--){ scanf("%d",&n); memset(vis,0,sizeof vis); f(i,0,n)scanf("%d",a + i); f(i,0,n)scanf("%d",s + a[i]); ans = 0; f(i,0,n)if (a[i] == s[ a[i] ])++ans; else if (!vis[ a[i] ]){ cnt = 0;tee = a[i]; while (!vis[tee]){ vis[tee] = 1; tee = s[tee]; ++cnt; } ans += cnt - 1; } printf("%d\n",ans); } return 0; } //1 5 3 2 6 4 //3 6 2 4 5 1 // //1 3 2 4 6 5 //3 2 4 1 5 6