#include <bits/stdc++.h>   using namespace std;   const long long maxm = 2e5 + 5;   vector<long long> g[maxm];   long long mark[maxm];   long long p[maxm];   long long c[maxm];   long long ans;   long long n;   void solve(long long x) {     vector<long long> s;     while (!mark[x]) {       s.push_back(x);       mark[x] = 1;       x = p[x];     }     long long len = s.size();     for (long long i = 1; i <= len; i++) {       if (len % i == 0) {         for (long long j = 0; j < i; j++) {           long long ok = 1;           for (long long k = j; k + i < len; k += i) {             if (c[s[k]] != c[s[k + i]]) {               ok = 0;               break;             }           }           if (ok) {             ans = min(ans, i);             return;           }         }       }     }   }   signed main() {     long long T;     cin >> T;     while (T--) {       cin >> n;       ans = n;       for (long long i = 1; i <= n; i++) cin >> p[i];       for (long long i = 1; i <= n; i++) cin >> c[i];       for (long long i = 1; i <= n; i++) mark[i] = 0;       for (long long i = 1; i <= n; i++)         if (!mark[i]) solve(i);       cout << ans << endl;     }     return 0;   }   