/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 300000;

int t;

int n;

int aParent[N_MAX + 2];
int bParent[N_MAX + 2];

vector <int> aSons[N_MAX + 2];
vector <int> bSons[N_MAX + 2];

int L[N_MAX + 2], R[N_MAX + 2];
int curr;

void dfs (int u)
{
    curr++;
    L[u] = R[u] = curr;
    for(int v : bSons[u])
    {
        dfs(v);
        R[u] = max(R[u], R[v]);
    }
}

struct SGTNode
{
    int maxVal;
    int lazy;
};

SGTNode join (const SGTNode &u, const SGTNode &v)
{
    return SGTNode{max(u.maxVal, v.maxVal), -1};
}

SGTNode SGT[N_MAX * 4 + 2];

int currAnswer;

struct Change
{
    SGTNode *ptr;
    SGTNode val;
    int prAnswer;
    int time;
};

vector <Change> changes;

struct ChangeBIT
{
    int *ptr;
    int val;
    int prAnswer;
    int time;
};

vector <ChangeBIT> changesBIT;

int currTime;

void pushChange (int node)
{
    changes.push_back(Change{&(SGT[node]), SGT[node], currAnswer, currTime});
}

void undo ()
{
    while(changes.empty() == false && changes.back().time > currTime)
    {
        *changes.back().ptr = changes.back().val;
        currAnswer = changes.back().prAnswer;
        changes.pop_back();
    }
    while(changesBIT.empty() == false && changesBIT.back().time > currTime)
    {
        *changesBIT.back().ptr = changesBIT.back().val;
        currAnswer = changesBIT.back().prAnswer;
        changesBIT.pop_back();
    }
}

void split (int node)
{
    if(SGT[node].lazy == -1)
        return;
    pushChange(node);
    int lSon = node * 2, rSon = node * 2 + 1;
    pushChange(lSon);
    pushChange(rSon);
    SGT[lSon].maxVal = SGT[node].lazy;
    SGT[lSon].lazy = SGT[node].lazy;
    SGT[rSon].maxVal = SGT[node].lazy;
    SGT[rSon].lazy = SGT[node].lazy;
    SGT[node].lazy = -1;
}

void build (int node, int l, int r)
{
    if(l == r)
    {
        SGT[node].maxVal = 0;
        SGT[node].lazy = -1;
        return;
    }
    int mid = (l + r) / 2;
    int lSon = node * 2, rSon = node * 2 + 1;
    build(lSon, l, mid);
    build(rSon, mid + 1, r);
    SGT[node] = join(SGT[lSon], SGT[rSon]);
}
void build ()
{
    build(1, 1, n);
}

void update (int node, int l, int r, int ul, int ur, int uval)
{
    if(ul <= l && r <= ur)
    {
        pushChange(node);
        SGT[node].maxVal = uval;
        SGT[node].lazy = uval;
        return;
    }
    split(node);
    int mid = (l + r) / 2;
    int lSon = node * 2, rSon = node * 2 + 1;
    if(ul <= mid)
        update(lSon, l, mid, ul, ur, uval);
    if(mid + 1 <= ur)
        update(rSon, mid + 1, r, ul, ur, uval);
    pushChange(node);
    SGT[node] = join(SGT[lSon], SGT[rSon]);
}
void update (int ul, int ur, int uval)
{
    update(1, 1, n, ul, ur, uval);
}

int query (int node, int l, int r, int qpos)
{
    if(l == r)
        return SGT[node].maxVal;
    split(node);
    int mid = (l + r) / 2;
    int lSon = node * 2, rSon = node * 2 + 1;
    if(qpos <= mid)
        return query(lSon, l, mid, qpos);
    else
        return query(rSon, mid + 1, r, qpos);
}
int query (int qpos)
{
    return query(1, 1, n, qpos);
}

int BIT[N_MAX + 2];

void updateBIT (int pos, int val)
{
    for(int i = pos; i <= n; i += i & -i)
    {
        changesBIT.push_back(ChangeBIT{&(BIT[i]), BIT[i], currAnswer, currTime});
        BIT[i] += val;
    }
}

int queryBIT (int pos)
{
    int answer = 0;
    for(int i = pos; i >= 1; i -= i & -i)
        answer += BIT[i];
    return answer;
}

void del (int u)
{
    update(L[u], R[u], 0);
    updateBIT(L[u], -1);
    currAnswer--;
}

void add (int u)
{
    update(L[u], R[u], u);
    updateBIT(L[u], +1);
    currAnswer++;
}

void push (int u)
{
    if(queryBIT(R[u]) - queryBIT(L[u] - 1) > 0)
        return;
    int q = query(L[u]);
    if(q > 0)
        del(q);
    add(u);
}

int answer;

void solve (int u)
{
    currTime++;
    push(u);
    answer = max(answer, currAnswer);
    for(int v : aSons[u])
    {
        solve(v);
    }
    currTime--;
    undo();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 2; i <= n; i++)
        {
            cin >> aParent[i];
            aSons[aParent[i]].push_back(i);
        }
        for(int i = 2; i <= n; i++)
        {
            cin >> bParent[i];
            bSons[bParent[i]].push_back(i);
        }
        build();
        for(int i = 1; i <= n; i++)
            BIT[i] = 0;
        curr = 0;
        dfs(1);
        answer = 0;
        currAnswer = 0;
        currTime = 0;
        solve(1);
        cout << answer << "\n";
        for(int i = 1; i <= n; i++)
        {
            aSons[i].clear();
            bSons[i].clear();
        }
        changes.clear();
        changesBIT.clear();
    }
    return 0;
}
