#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
#include <ctime>

using namespace std;

#define x first
#define y second
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define SZ(x) int(x.size())
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = b-1; i >= a; i--)
#define DBG(x) cerr << (#x) << "=" << x << "\n";
#define ll long long
#define inf 1000000007
#define mod 1000000007
#define N 300005

template<typename U, typename V> void Min(U &a, const V & b){if(a > b)a = b;}
template<typename U, typename V> void Max(U &a, const V & b){if(a < b)a = b;}
template<typename U, typename V> void add(U &a, const V & b){a = (a + b) % mod;}
template<typename U> U gcd(U a, U b){
    if(a == 0)return b;
    if(b == 0)return a;
    if(a >= b)return gcd(a % b, b);
    else return gcd(a, b % a);
}
int pow(int a, int b){
    int ans = 1;
    while(b){
        if(b & 1)ans = 1LL * ans * a % mod;
        a = 1LL * a * a % mod;
        b >>= 1;
    }
    return ans;
}
int pow(int a, ll b, int c){
    int ans = 1;
    while(b){
        if(b & 1)ans = 1LL * ans * a % c;
        a = 1LL * a * a % c;
        b >>= 1;
    }
    return ans;
}

vi g[2][N];
int st[N], ed[N], cnt, ans;
void dfs(int u) {
  st[u] = ++cnt;
  for (auto &v : g[1][u]) {
    dfs(v);
  }
  ed[u] = cnt;
}
set<pii> s;
void go(int u) {
  auto it = s.lower_bound(mp(st[u], 0));
  int replace = -1;
  if(it != s.begin()) {
    --it;
    int x = it->y;
    if (ed[x] >= ed[u]) {
      replace = x;
      s.erase(it);
    }
  }
  s.insert({st[u], u});
  Max(ans, SZ(s));
  for (auto &v : g[0][u]) {
    go(v);
  }
  s.erase({st[u], u});
  if (replace != -1) {
    s.insert({st[replace], replace});
  }
}
int main(){
  int ca = 0, T, i, j, k, K, m, n;
  scanf("%d", &T);
  while(T--) {
    scanf("%d", &n);
    rep(i, 0, n) rep(j, 0, 2)g[j][i].clear();
    rep(k, 0, 2)
    rep(i, 1, n) {
      scanf("%d", &j); j--;
      g[k][j].pb(i);
    }
    cnt = 0;
    dfs(0);
    s.clear();
    ans = 0;
    go(0);
    printf("%d\n", ans);
  }
}
