#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int mxn = 300001;
int n;
pi a[mxn];
vector<int> g[2][mxn];
set<pi> s;

int dfs(int c){
	a[c].s = a[c].f;
	for(int i : g[1][c]) a[i].f = a[c].s + 1, a[c].s = dfs(i);
	return a[c].s;
}

int dfs2(int c){
	pi x = {-1, -1};
	auto it = s.insert(a[c]).f;
	if(it != s.begin()){
		it--;
		if(a[c].f <= it->s) x = *it, s.erase(it);
	}
	int ret = s.size();
	for(int i : g[0][c]) ret = max(ret, dfs2(i));
	s.erase(a[c]);
	if(~x.f) s.insert(x);
	return ret;
}

void answer(){
	cin >> n;
	
	for(int i = 0; i < 2; i++)
	for(int j = 0; j < n; j++){
		g[i][j].clear();
		if(j){
			int x;
			cin >> x;
			g[i][--x].push_back(j);
		}
	}
	
	dfs(0);
	
	cout << dfs2(0) << endl;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int t;
	cin >> t;
	
	for(int i = 0; i < t; i++) answer();

	return 0;
}