#include <bits/stdc++.h>
#include <stdlib.h>

using namespace std;

typedef  long long  int ll;
typedef  long double ld;

#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ln length()
#define M 1000000007

ll n, p1[300004],p2[300004],bit1[600004],bit2[600004];

ll st2[300004],fin2[300004], dfscntr2;

vll ch1[300005],ch2[300005];

ll ans,finans;

void dfs(ll st) {
    st2[st] = ++dfscntr2;
    for(ll v : ch2[st]) dfs(v);
    fin2[st] = ++dfscntr2;
}

void updateb1(ll p,ll v){
    for(ll i=p;i<=2*n;i+=(i&-i)) bit1[i] += v;
}
ll qryb1(ll p){
    ll ret=0;
    for(ll i=p;i;i-=(i&-i)) ret+=bit1[i];
    return ret;
}

ll sumb1(ll l, ll r) {
    return qryb1(r) - qryb1(l-1);
}

void updateb2(ll p,ll v){
    for(ll i=p;i<=2*n;i+=(i&-i)) bit2[i] += v;
}
ll qryb2(ll p){
    ll ret=0;
    for(ll i=p;i;i-=(i&-i)) ret+=bit2[i];
    return ret;
}

ll sumb2(ll l, ll r) {
    return qryb2(r) - qryb2(l-1);
}


void findans(ll v) {

    ll curans = ans;
    ll par = 0;
    if (v == 1) {
        ans = 1;
    } else {
        par = sumb2(1, st2[v]-1);
        if(sumb1(st2[v], fin2[v]) == 0) {
            if(!par) ans++;
            else if (sumb1(st2[par], fin2[par]) > 1) ans++;
        }
    }
    if(par) {
        updateb2(st2[par], -par);
        updateb2(fin2[par], par);
    }
    // add
    updateb1(st2[v], 1);
    updateb2(st2[v], v);
    updateb2(fin2[v], -v);


    finans = max(finans, ans);


    for(ll vv : ch1[v]) findans(vv);

    ans = curans;
    // removal

    if(par) {
        updateb2(st2[par], par);
        updateb2(fin2[par], -par);
    }

    updateb1(st2[v], -1);
    updateb2(st2[v], -v);
    updateb2(fin2[v], v);

}

int main() {
    ios :: sync_with_stdio(false); cin.tie(0);

    ll t;cin>>t;while(t--) {
        cin>>n;

        rep(i,1,n) {
            ch1[i].clear();
            ch2[i].clear();
            bit1[i] = bit2[i] = 0;
        }
        dfscntr2 = ans = finans = 0;

        rep(i,2,n) {cin>>p1[i]; ch1[p1[i]].pb(i);}
        rep(i,2,n) {cin>>p2[i]; ch2[p2[i]].pb(i);}

        dfs(1);

        findans(1);
        cout<<finans<<'\n';
    }
}
