#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9+10;
const ll LINF = 1LL*INF*INF;
const int MAXN = 3e5+10;
const int MAXM = 5e3+10;

priority_queue<int> pq;
vector<vector<int> > graph;
vector<vector<int> > tgraph;
queue<int> que;

int A[MAXN];
int B[MAXN];
int subtree[MAXN];
int top[MAXN];
int par[MAXN];

int in[MAXN];
int out[MAXN];

int mxtree[MAXN*4];
int tree[MAXN*4];
int ans[MAXN];
int er[MAXN];

int seq = 0;

int base = 1;

void getsub(int here)
{
    int there;

    subtree[here] = 1;

    for(int i=0;i<tgraph[here].size();i++){
        there = tgraph[here][i];
        getsub(there);
        subtree[here]+=subtree[there];

        if(subtree[tgraph[here][0]]<subtree[there]){
            swap(tgraph[here][0],tgraph[here][i]);
        }
    }

    return;
}

void gettop(int here)
{
    int there;

    in[here] = seq;
    seq++;

    for(int i=0;i<tgraph[here].size();i++){
        there = tgraph[here][i];
        if(!i)top[there] = top[here];
        else top[there] = there;
        par[there] = here;
        gettop(there);
    }

    out[here] = seq-1;

    return;
}

int getpar(int here)
{
    int L,R;
    int tt;
    int mx = -INF;

    while(here){
        tt = top[here];
        L = base+in[tt];
        R = base+in[here];

        while(L<=R){
            if(L&1){
                mx = max(mx,mxtree[L]);
                L++;
            }
            if(!(R&1)){
                mx = max(mx,mxtree[R]);
                R--;
            }
            L>>=1; R>>=1;
        }

        if(mx!=-INF){
            break;
        }
        here = par[tt];
    }

    if(mx==-INF)return -1;
    else return mx;
}

void myadd(int here)
{
    int tmp = base+in[here];
    mxtree[tmp] = here;
    tree[tmp] = 1;
    tmp>>=1;
    while(tmp){
        mxtree[tmp] = max(mxtree[(tmp<<1)],mxtree[(tmp<<1)|1]);
        tree[tmp]++;
        tmp>>=1;
    }

    return;
}

void myer(int here)
{
    int tmp = base+in[here];
    mxtree[tmp] = -INF;
    tree[tmp] = 0;
    tmp>>=1;
    while(tmp){
        mxtree[tmp] = max(mxtree[(tmp<<1)],mxtree[(tmp<<1)|1]);
        tree[tmp]--;
        tmp>>=1;
    }

    return;
}


void solve(int here,int v)
{
    int there;

    int tmp;
    int L = base+in[here];
    int R = base+out[here];

    int sum = 0;

    while(L<=R){
        if(L&1){
            sum+=tree[L];
            L++;
        }
        if(!(R&1)){
            sum+=tree[R];
            R--;
        }
        L>>=1; R>>=1;
    }

    if(!sum){

        int p = getpar(here);

        if(p==-1){
            v++;
        }
        else {
            myer(p);
        }
        myadd(here);
        er[here] = p;
    }
    else {
        er[here] = -1;
    }

    ans[here] = v;

    for(int i=0;i<graph[here].size();i++){
        there = graph[here][i];
        solve(there,v);
    }

    if(!sum){
        myer(here);
        if(er[here]!=-1)myadd(er[here]);
    }

    return;
}

int main()
{
    int n,m,k,a,b,x,y,q;
    int sum = 0;
    int cnt = 0;
    int mx = 0;
    int mn = INF;
    int cur = 0, idx = -1;
    int tc;
    int res = 0;

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>tc;

    while(tc--){

        cin>>n;

        for(int i=2;i<=n;i++){
            cin>>A[i];
        }
        for(int i=2;i<=n;i++){
            cin>>B[i];
        }

        graph = vector<vector<int> > (n+1);
        tgraph = vector<vector<int> > (n+1);

        for(int i=2;i<=n;i++){
            graph[A[i]].push_back(i);
            tgraph[B[i]].push_back(i);
        }
        seq = 0;

        getsub(1);
        top[1] = 1;
        gettop(1);

        base = 1;
        for(;base<n;base<<=1);

        for(int i=1;i<(base<<1);i++){
            tree[i] = 0;
            mxtree[i] = -INF;
        }

        solve(1,0);

        res = 0;
        for(int i=1;i<=n;i++){
            res = max(res,ans[i]);
        }
        cout<<res<<"\n";
    }

    return 0;
}
