#include<bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
template<class T1,class T2,class T3>T1 ksm(T1 a,T2 b,T3 mod){T1 ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
#ifndef ONLINE_JUDGE
template<class T>T __gcd(T a,T b){ll r;while(b>0){r=a%b;a=b;b=r;}return a;}
#endif
#define inf INT_MAX
#define Max(x,y) x=max(x,y)
#define Min(x,y) x=min(x,y)
#define Out(t) printf("%s\n",(t)?"YES":"NO")
const double pi=acos(-1.);
const int def=300010;
const int mod=1000000007;
using namespace std;

struct segtree{
	#define lc (now<<1)
	#define rc (now<<1|1)

	ll lazy[def<<2];
	ll val[def<<2];
	int l=0,r=0;

	void init(int l,int r)
	{
		this->l=l;
		this->r=r;
		build(l,r);
	}

	void pushdown(int now)
	{
		if(~lazy[now]){
			if(lc<(def<<2))lazy[lc]=lazy[now];
			if(rc<(def<<2))lazy[rc]=lazy[now];
			val[now]=lazy[now];
			lazy[now]=-1;
		}
	}

	void pushup(int now)
	{	int mid=(l+r)/2;
		pushdown(lc);pushdown(rc);
		val[now]=max(val[lc],val[rc]);
	}

	void build(int l,int r,int now=1)
	{
		lazy[now]=-1;
		if(l==r)val[now]=0;
		else{
			int mid=(l+r)/2;
			build(l,mid,lc);
			build(mid+1,r,rc);
			pushup(now);
		}
	}

	void upd(int x,ll z){upd(l,r,x,x,z);}//单点修改
	void upd(int x,int y,ll z){upd(l,r,x,y,z);}//区间修改
	void upd(int l,int r,int x,int y,ll z,int now=1)//add or change
	{
		pushdown(now);
		if(x<=l&&r<=y)
			lazy[now]=z;
		else{
			int mid=(l+r)/2;
			if(x<=mid)
				upd(l,mid,x,y,z,lc);
			if(mid<y)
				upd(mid+1,r,x,y,z,rc);
			pushup(now);
		}
	}

	ll query(int x,int y){return query(l,r,x,y);}
	ll query(int l,int r,int x,int y,int now=1)
	{
		pushdown(now);
		if(x<=l&&r<=y)
			return val[now];
		else{
			int mid=(l+r)/2;
			ll ans=0;
			if(x<=mid)
				ans=max(ans,query(l,mid,x,y,lc));
			if(mid<y)
				ans=max(ans,query(mid+1,r,x,y,rc));
			return ans;
		}
	}
}tr;

vector<int>mp1[def],mp2[def];
int ans,l[def],r[def],cnt;

void dfs1(int now)
{
	l[now]=++cnt;
	for(auto i:mp2[now])
		dfs1(i);
	r[now]=cnt;
}

void dfs2(int now,int num)
{
	int ori=tr.query(l[now],r[now]);
	if(ori)
		tr.upd(l[ori],r[ori],0);
	else
		num++;
	tr.upd(l[now],r[now],now);
	for(auto i:mp1[now])
		dfs2(i,num);
	tr.upd(l[now],r[now],0);
	if(ori)
		tr.upd(l[ori],r[ori],ori);
	Max(ans,num);
}

int main()
{	int _=1,__=1,n,x;
	for(((1)?scanf("%d",&_):EOF);_;_--,__++){
		scanf("%d",&n);
		tr.init(1,n);
		for(int i=1;i<=n;i++){
			mp1[i].clear();
			mp2[i].clear();
		}
		for(int i=2;i<=n;i++){
			scanf("%d",&x);
			mp1[x].push_back(i);
		}
		for(int i=2;i<=n;i++){
			scanf("%d",&x);
			mp2[x].push_back(i);
		}
		cnt=ans=0;
		dfs1(1);
		dfs2(1,0);
		printf("%d\n",ans);
	}
	return 0;
}