#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define D(x) {x;}
#else
#define D(x)
#endif
using ll = long long;
const int N = 3e5;
vector<int> vs[N], vk[N];
pair<int, int> kat[N];
int kt = 0;
void kh(int i) {
  kat[i].first = kt++;
  for (int j : vk[i]) kh(j);
  kat[i].second = kt++;
}
map<int, int> act;
int res = 0;
void sh(int i) {
  auto [x, y] = kat[i];
  auto it = act.upper_bound(x);
  bool ring = 0;
  pair<int, int> rv{};
  if (it != act.begin()) {
    it--;
    if (it->second > x) {
      ring = 1;
      rv = *it;
      act.erase(it);
    }
  }
  act.insert(kat[i]);
  res = max(res, (int)act.size());
  for (int j : vs[i]) sh(j);
  act.erase(x);
  if (ring) {
    act.insert(rv);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int ti = 0; ti < t; ++ti) {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      vs[i].clear();
      vk[i].clear();
    }
    for (int i = 1; i < n; ++i) {
      int a;
      cin >> a;
      vs[a-1].push_back(i);
    }
    for (int i = 1; i < n; ++i) {
      int a;
      cin >> a;
      vk[a-1].push_back(i);
    }
    kt = 0;
    kh(0);
    act.clear();
    res = 0;
    sh(0);
    cout << res << endl;
  }
}
