//This amazing code is written by Primo.Jay.Pan From CUC

//Any questions please contact me
//QQ:898021802
//Mail: primojpan@gmail.com
//.    primojaypan@cuc.edu.cn


// CF1258C.cpp
// Created by Primo Jay Pan on 2021/5/26.
//While (true) RP++;


//head
#include <iostream>
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 0x3f3f3f3f
#define maxlongint 2147483647
#define maxint 32767
#define pi(double)acos(-1.0)
#define eps 1e-9
#define MID ((l+r)>>1)
#define lson (o<<1)
#define rson (o<<1|1)
//clear the array
#define cla(a) memset(a,0,sizeof(a))
#define clb(a) memset(a,INF,sizeof(a))
#define clc(a) memset(a,-1,sizeof(a))
#define maxn (int)((3e5)+10)
#pragma GCC optimize ("O2,unroll-loops")


using namespace std;


//BIT
inline int lowbit(int x)
{
	return x&(-x);
}

//var

vector<int> G1[maxn],G2[maxn];
int T,n;
set< pii > S;
int L[maxn],R[maxn];
int Clo=0;
int ans=0;
//main
inline void dfs1(int u)
{
	L[u]=Clo++;
	for (int i=0;i<G2[u].size();i++){
		int v=G2[u][i];
		dfs1(v);
	}
	R[u]=Clo;
	
}
inline int get(int u)
//-2表示u在S中有后代，不放u进去
//-1表示u在S中没有祖先
//otherwise返回祖先的id,并且删除
{
	set< pii >::iterator it=S.lower_bound(mp(L[u],u));
	if (it!=S.end() && R[it->second]<=R[u])
		return -2;//u在当前集合中有儿子，所以u不选
	if (it==S.begin())
		return -1;//为第一个元素，没有父元素
	--it;//迭代找父亲
	int Fa=it->second;
	if (R[u]>R[Fa])//不是祖先
		return -1;
	S.erase(it);
	return Fa;
}
inline void dfs2(int u)
{
	int Fa=get(u);
	if (Fa!=-2)
		S.insert(mp(L[u],u));
	ans=max(ans,(int)S.size());
	for (int i=0;i<G1[u].size();i++){
		int v=G1[u][i];
		dfs2(v);
	}
	if (Fa!=-2)
	{
		S.erase(mp(L[u],u));
		if (Fa!=-1)
			S.insert(mp(L[Fa],Fa));
	}
}
int main()
{
	
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>T;
	while (T--)
	{
		cin>>n;
		for (int i=1;i<=n;i++)
		{
			G1[i].clear();
			G2[i].clear();
		}
		for (int i=2;i<=n;i++)
		{
			int x;
			cin>>x;
			G1[x].push_back(i);
		}
		for (int i=2;i<=n;i++)
		{
			int x;
			cin>>x;
			G2[x].push_back(i);
		}
		Clo=1;
		dfs1(1);
		ans=0;
		dfs2(1);
		cout<<ans<<endl;
	}
	return 0;
}



/*
Test Data1:
Test Data2:
*/