#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MS(a, b) memset(a, b, sizeof a) #define LB(x) ((x) & (-x)) #define REP(a, b, c) for(int a = b; a < c; ++a) #define REP1(a, b, c) for(int a = b; a <= c; ++a) using namespace std; struct node{ int a, b; node(int a = 0, int b = 0) : a(a), b(b) {} bool operator < (const node & v) const {return a < v.a;} }d[100005]; int n; bool flag[100005]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1;i<=n;i++) scanf("%d",&d[i].a); for(int i = 1;i<=n;i++) scanf("%d",&d[i].b); sort(d+1,d+n+1); MS(flag, true); int tot = 0; for(int i = 1;i<=n;i++) if(flag[i]) { int p = d[i].b; if(p!=i) tot++; while(flag[p]) {flag[p]=false;p = d[p].b;} } printf("%d\n",n-tot); } return 0; }