// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define rep(i, init, n) for (int i = init; i < (int)n; i++)
#define rev(i, n, init) for (int i = (int)n; i >= init; i--)
#define MOD1 1e9 + 7
#define MOD2 998244353
#define f first
#define s second
#define endl '\n'
#define pii pair<int, int>
#define tii tuple<int, int>
#define all(v) v.begin(), v.end()
#define mt make_tuple
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
#define hash hash1
#define data data1
#define prev prev1
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int myRand(int R)
{
     int val = rng() % R;
     assert(val >= 0);
     return val;
}

const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
int32_t MOD = MOD1;

using namespace std;

//-------------------DEBUGGING-------------------------
void my_debugger(string s, int LINE_NUM) { cerr << endl; }
template <typename start, typename... end>
void my_debugger(string s, int LINE_NUM, start x, end... y)
{
     if (s.back() != ',')
     {
          s += ',';
          cerr << "LINE(" << LINE_NUM << "): ";
     }
     int i = s.find(',');
     cerr << s.substr(0, i) << " = " << x;
     s = s.substr(i + 1);
     if (!s.empty())
          cerr << ", ";
     my_debugger(s, LINE_NUM, y...);
}

#ifdef AJIT
#define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__);
#else
#define debug(...) ;
#endif

void setMod(int mod_val)
{
     MOD = mod_val;
}
//-----------------------------------------------------

void files_init()
{
     //freopen("file.in", "r", stdin);
     //freopen("factorials.txt", "w", stdout);
}

const int N = 2e5+5;
const int LOGN = 20;


int32_t main()
{
     getFaster;
     //files_init();
     int tests = 1;
     cin >> tests;
     rep(ii, 0, tests)
     {
           int n;
           cin>>n;
           vector<int> a(n+1),b(n+1);
           rep(i,2,n+1)
           cin>>a[i];
           rep(i,2,n+1)
           cin>>b[i];

           vector<vector<int>> g1(n+1);
           vector<vector<int>> g2(n+1);
           vector<int> tin(n+1),tout(n+1);
           vector<vector<pair<int,pii>>> updates(n+1);
           //0 or remove
           //1 for insert

           rep(i,2,n+1)
           {
                g1[a[i]].push_back(i);
                g2[b[i]].push_back(i);
           } 

           int timer=0;
           function<void(int)> dfs1=[&](int i)
           {
                 tin[i]=++timer;
                 for(auto it:g2[i])
                 dfs1(it);
                 tout[i]=++timer;
           };

           dfs1(1);

           set<pii> best;

           auto getCandAncestor = [&](int l,int r) -> pii
           {
                 if(best.empty())
                 return {-1,-1};

                 auto it=best.upper_bound({l,-1});
                 if(it==best.begin())
                 return {-1,-1};

                 it--;
                 int l1=it->f,r1=it->s;

                 if(l1<=l && r<=r1)
                 return {l1,r1};

                 return {-1,-1};
           };

           auto hasCandChild = [&](int l,int r) -> bool
           {
                auto it=best.lower_bound({l,-1});
                if(it==best.end())
                return false;

                int l1=it->f,r1=it->s;
                if(l<=l1 && r1<=r)
                return true;

                return false;
           };

           int ans=0;

           function<void(int)> dfs2=[&](int i)
           {
                 int l=tin[i],r=tout[i];
                 
                 if(!hasCandChild(l,r))
                 {
                      pii p=getCandAncestor(l,r);
                      if(p.f!=-1)
                      {
                           best.erase(p);
                           updates[i].push_back({0,p});
                      }
                      best.insert({l,r});
                      updates[i].push_back({1,{l,r}});
                 }

                 ans=max(ans,(int)best.size());

                 for(auto it:g1[i])
                 dfs2(it);

                 for(auto it:updates[i])
                 {
                      if(it.f==0)
                      best.insert(it.s);
                      else
                      best.erase(it.s);
                 }
                 updates[i].clear();
           };

           dfs2(1);
           cout<<ans<<endl;
     }
     return 0;
}