// E. Trees of Tranquillity
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[])
{
	cin.tie(0)->sync_with_stdio(0);
	int T;
	cin >> T;
	while (T--) {
		int n, time = 0;
		cin >> n;
		vector<vector<int>> a(n), b(n);
		for (int v = 1, u; v < n; v++) cin >> u, a[u - 1].push_back(v);
		for (int v = 1, u; v < n; v++) cin >> u, b[u - 1].push_back(v);

		vector<int> in(n), out(n);
		function<void(int)> dfs1 = [&](int u) {
			in[u] = time++;
			for (auto v : b[u]) dfs1(v);
			out[u] = time++;
		};
		dfs1(0);

		set<pair<int, int>> s;
		int ans = 0, now = 0;

		auto is_parent = [&](int u, int v) { return in[u] <= in[v] && out[u] >= out[v]; };

		function<void(int)> dfs2 = [&](int u) {
			int last = now;
			auto it = s.lower_bound({in[u], 0});
			if (it != s.end() && !is_parent(u, it->second)) now++;
			if (it != s.begin()) {
				auto it2 = it--;
				if (!is_parent(it->second, u)) now++;
				if (it2 != s.end() && !is_parent(it->second, it2->second)) now--;
			}
			// if (it != s.end()) now += 1 - (is_parent(u, it->second) ? 1 : 0);
			// if (it != s.begin()) {
			// 	auto it2 = it--;
			// 	now += 1 - (is_parent(it->second, u) ? 1 : 0);
			// 	if (it2 != s.end()) now -= 1 - (is_parent(it->second, it2->second) ? 1 : 0);
			// }
			s.insert({in[u], u});
			ans = max(ans, now);
			for (auto v : a[u]) dfs2(v);
			s.erase({in[u], u});
			now = last;
		};

		dfs2(0);
		cout << ans + 1 << endl;
	}

	return 0;
}
