#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<queue>
#include<complex>
#include<set>
#define int long long
//#define double long double
using namespace std;
using pii = pair<int, int>;
const int inf = 1e9 + 7;
struct segtree {
	vector<int> t;
	vector<int> mod;
	segtree() {
		t = {};
		mod = {};
	}
	segtree(int n, int x) {
		t = vector<int>(4 * n, x);
		mod = vector<int>(4 * n, -inf);
	}
	void push(int v, int tl, int tr) {
		//cout << v << ' ' << tl << ' ' << tr << endl;
		if (tl >= tr || mod[v] == -inf) return;
		int tm = (tl + tr) >> 1;
		t[2 * v] = mod[v] * (tm - tl + 1);
		t[2 * v + 1] = mod[v] * (tr - tm);
		mod[2 * v] = mod[v];
		mod[2 * v + 1] = mod[v];
		mod[v] = -inf;
	}
	int get(int v, int tl, int tr, int l, int r) {
		push(v, tl, tr);
		if (tl > tr || l > r) return 0;
		if (tl == l && tr == r) {
			return t[v];
		}
		int tm = (tl + tr) >> 1;
		return (get(2 * v, tl, tm, l, min(r, tm)) + get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
	}
	void upd(int v, int tl, int tr, int l, int r, int x) {
		push(v, tl, tr);
		if (tl > tr || l > r) return;
		if (tl == l && tr == r) {
			mod[v] = x;
			t[v] = x * (tr - tl + 1);
			return;
		}
		int tm = (tl + tr) >> 1;
		upd(2 * v, tl, tm, l, min(r, tm), x);
		upd(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, x);
		t[v] = t[2 * v] + t[2 * v + 1];
	}
	void add(int v, int tl, int tr, int x, int ps) {
		//cout << tl << ' ' << tr << ' ' << ps << endl;
		push(v, tl, tr);
		if (tl == tr) {
		//	cout << "OK\n";
			t[v] += x;
			return;
		}
		int tm = (tl + tr) >> 1;
		if (ps <= tm) {
			add(2 * v, tl, tm, x, ps);
		} else {
			add(2 * v + 1, tm + 1, tr, x, ps);
		}
		t[v] = t[2 * v] + t[2 * v + 1];
	}
};
vector<vector<int>> g1, g2;
vector<int> tin1, tout1, tin2, tout2;
int tm1 = 0, tm2 = 0;
void dfs1(int v, int p = -1) {
	tin1[v] = tm1++;
	for (auto u : g1[v]) {
		if (u != p) {
			dfs1(u, v);
		}
	}
	tout1[v] = tm1;
}
void dfs2(int v, int p = -1) {
	tin2[v] = tm2++;
	for (auto u : g2[v]) {
		if (u != p) {
			dfs2(u, v);
		}
	}
	tout2[v] = tm2;
}
segtree t1, t2;
int ANS = 0;
int curval = 0;
void dfsres(int v,int n,int p = -1) {
	int p0 = -1;
	bool ok = 0;
	int add = 0;
	if (t1.get(1, 0, n - 1, tin2[v], tout2[v] - 1) == 0) {
		ok = 1;
		p0 = t2.get(1, 0, n - 1, tin2[v], tin2[v]);
		if (p0 == -1) {
			curval++;
			add = 1;
		}
		if (p0 != -1) {
			t2.upd(1, 0, n - 1, tin2[p0], tout2[p0] - 1, -1);
		}
		t2.upd(1, 0, n - 1, tin2[v], tout2[v] - 1, v);
	}
	t1.add(1, 0, n - 1, 1, tin2[v]);
	ANS = max(ANS, curval);
	for (auto u : g1[v]) {
		if (u == p) continue;
		dfsres(u, n, v);
	}
	t1.add(1, 0, n - 1, -1, tin2[v]);
	if (ok) {
		t2.upd(1, 0, n - 1, tin2[v], tout2[v] - 1, -1);
	}
	if (p0 != -1) {
		t2.upd(1, 0, n - 1, tin2[p0], tout2[p0] - 1, p0);
	}
	curval -= add;
}
void solve() {
	int n;
	cin >> n;
	t1 = segtree(n, 0);
	t2 = segtree(n, -1);
	g1.clear();
	g2.clear();
	tin1.clear();
	tin2.clear();
	tout1.clear();
	tout2.clear();
	tm1 = 0, tm2 = 0;
	g1.resize(n);
	g2.resize(n);
	tin1.resize(n);
	tin2.resize(n);
	tout1.resize(n);
	tout2.resize(n);
	for (int i = 1; i < n; ++i) {
		int x;
		cin >> x;
		g1[x - 1].push_back(i);
	}
	for (int i = 1; i < n; ++i) {
		int x;
		cin >> x;
		g2[x - 1].push_back(i);
	}
	ANS = 0;
	dfs1(0);
	dfs2(0);
	dfsres(0, n);
	cout << ANS << '\n';
}
int32_t main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int tt = 1;
    std::cin >> tt;
    while (tt--) {
        solve();
    }
}