#include #include #include using namespace std; int a[100005], b[100005], ind_a[100005], ind_b[100005]; bool used[100005] = {false}; int main() { int t, n, ans; scanf("%d", &t); while(t--) { scanf("%d", &n); memset(used, false, sizeof(used)); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); ind_a[a[i]] = i; } for(int i = 1; i <= n; i++) { scanf("%d", &b[i]); ind_b[b[i]] = i; } ans = 0; for(int i = 1; i <= n; i++) { int p = a[i]; if(!used[b[ind_a[p]]]) { used[b[ind_a[p]]] = true; if(p == b[ind_a[p]]) ans++; while(!used[p]) { used[p] = true; ans++; p = a[ind_b[p]]; } } } printf("%d\n", ans); } return 0; }