#include<iostream> #include<cstdlib> #include<cstdio> #include<vector> #include<cmath> usingnamespacestd; inlineintread() { int f = 1, x = 0; char ch;
do{ ch = getchar(); if (ch == '-') f = -1; }while(ch < '0' || ch > '9'); do{ x = x * 10 + ch - '0'; ch = getchar(); }while(ch >= '0' && ch <= '9'); return f * x; } constint N = 2e5;
int tt; int n; int p[N + 1], c[N + 1]; int d[N + 1]; int ans; vector<int> v;
intmain() { tt = read();
while (tt--) { n = read(); for (int i = 1; i <= n; i++) { d[i] = 0; p[i] = read(); } for (int i = 1; i <= n; i++) c[i] = read(); ans = n; for (int i = 1; i <= n; i++) { if (!d[i]) { v.clear(); int now = i; while (!d[now]) { d[now] = 1; v.push_back(now); now = p[now]; } bool flag = false; for (int j = 1; j <= v.size(); j++) { if (v.size() % j) continue; for (int k = 0; k < j; k++) { flag = true; for (int now = k; now + j < v.size(); now += j) { if (c[v[now]] != c[v[now + j]]) { flag = false; break; } } if (flag) break; } if (flag) { ans = min(ans, j); break; } } } } printf("%d\n", ans); }