#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 3e5 + 20;

vector<int> g[N], h[N];
int tin[N], tout[N], timer, ans;

void dfs1(int v, int p){
    tin[v] = ++timer;
    for(int x : h[v])
        if(x != p)
            dfs1(x, v);
    tout[v] = ++timer;
}

set<pair<int, int>> s;
void dfs2(int v, int p){
    bool in = false;
    pair<int, int> rem = {-1, -1};
    auto it = (s.empty() ? s.end() : --s.upper_bound({tin[v], -1}));
    if(it != s.end() && it->first <= tin[v] && it->second >= tout[v]){
        rem = *it;
        s.erase(it);
    }
    it = s.lower_bound({tin[v], -1});
    if(!(it != s.end() && tin[v] <= it->first && tout[v] >= it->second)){
        s.insert({tin[v], tout[v]});
        in = true;
    }
    ans = max(ans, (int)s.size());
    for(int x : g[v])
        if(x != p)
            dfs2(x, v);
    if(in)
        s.erase({tin[v], tout[v]});
    if(rem != make_pair(-1, -1))
        s.insert(rem);
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while(t --){
        int n; cin >> n;
        for(int i = 0; i < n; i ++){
            g[i].clear();
            h[i].clear();
        }
        timer = ans = 0;
        for(int i = 0; i < n - 1; i ++){
            int v; cin >> v;
            -- v;
            g[v].push_back(i + 1);
        }
        for(int i = 0; i < n - 1; i ++){
            int v; cin >> v;
            -- v;
            h[v].push_back(i + 1);
        }
        dfs1(0, 0);
        dfs2(0, 0);
        cout << ans << "\n";
    }
}