#include <bits/stdc++.h>

using namespace std;

const int MAXN = 300005;

struct Node {
  pair<int, int> v;
  int t;
  Node *ls, *rs;
  void clear() {
    t = 0;
    v = make_pair(0, 0);
    ls = rs = nullptr;
  }
  void pushUp() {
    if (ls->v.first == rs->v.first) {
      v = make_pair(ls->v.first, ls->v.second + rs->v.second);
    } else {
      v = min(ls->v, rs->v);
    }
  }
  void update(int ft) {
    v.first += ft;
    t += ft;
  }
  void pushDown() {
    if (t) {
      ls->update(t);
      rs->update(t);
      t = 0;
    }
  }
}pool[MAXN<<2];
 
int node_cnt = 0;
 
void clear() {
  node_cnt = 0;
}
 
Node *newNode() {
  pool[node_cnt].clear();
  return &pool[node_cnt++];
}
 
void add(Node *cur, int l, int r, int a, int b, int v) {
  if (a > b) swap(a, b);
  if (a <= l && b >= r) {
    cur->update(v);
  } else {
    cur->pushDown();
    int mid = (l + r) / 2;
    if (a <= mid) {
      add(cur->ls, l, mid, a, b, v);
    }
    if (b > mid) {
      add(cur->rs, mid + 1, r, a, b, v);
    }
    cur->pushUp();
  }
}
 
pair<int, int> query(Node *cur, int l, int r, int a, int b) {
  if (a > b) swap(a, b);
  if (a <= l && b >= r) {
    return cur->v;
  }
  cur->pushDown();
  int mid = (l + r) / 2;
  if (b <= mid) {
    return query(cur->ls, l, mid, a, b);
  }
  if (a > mid) {
    return query(cur->rs, mid + 1, r, a, b);
  }
  pair<int, int> x = query(cur->ls, l, mid, a, b);
  pair<int, int> y = query(cur->rs, mid + 1, r, a, b);
  if (x.first == y.first) {
    return make_pair(x.first, x.second + y.second);
  }
  return min(x, y);
}
 
Node *build(int l, int r) {
  Node *cur = newNode();
  if (l < r) {
    int mid = (l + r) / 2;
    cur->ls = build(l, mid);
    cur->rs = build(mid + 1, r);
    cur->pushUp();
  } else {
    cur->t = 0;
    cur->v = make_pair(0, 1);
  }
  return cur;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    clear();
    int n;
    cin >> n;
    vector<vector<int>> G1(n + 1);
    vector<vector<int>> G2(n + 1);
    for (int i = 2; i <= n; i++) {
      int x;
      cin >> x;
      G1[x].push_back(i);
    }
    for (int i = 2; i <= n; i++) {
      int x;
      cin >> x;
      G2[x].push_back(i);
    }
    vector<int> depth(n + 1);
    vector<int> son(n + 1);
    vector<int> size(n + 1);
    vector<int> father(n + 1);
    function<void(int, int, int)> dfs1 = [&](int cur, int fa, int dep) -> void {
      depth[cur] = dep;
      size[cur] = 1;
      father[cur] = fa;
      for (auto nx: G2[cur]) {
        if (nx == fa) {
          continue;
        }
        dfs1(nx, cur, dep + 1);
        size[cur] += size[nx];
        if (size[nx] > size[son[cur]]) {
          son[cur] = nx;
        }
      }
    };
    vector<int> top(n + 1);
    vector<int> dfn(n + 1);
    vector<int> low(n + 1);
    int dfs_clock = 0;
    function<void(int, int)> dfs2 = [&](int cur, int tp) -> void {
      top[cur] = tp;
      dfn[cur] = ++dfs_clock;
      if (son[cur]) {
        dfs2(son[cur], tp);
      }
      for (auto nx: G2[cur]) {
        if (nx == father[cur] || nx == son[cur]) {
          continue;
        }
        dfs2(nx, nx);
      }
      low[cur] = dfs_clock;
    };
    dfs1(1, 0, 0);
    dfs2(1, 1);
    Node *root = build(1, n);
    auto path_add = [&](int x, int y, int v) -> void {
      int t1 = top[x];
      int t2 = top[y];
      while (t1 != t2) {
        if (depth[t1] < depth[t2]) {
          swap(t1, t2);
          swap(x, y);
        }
        add(root, 1, n, dfn[x], dfn[t1], v);        
        x = father[t1];
        t1 = top[x];
      }
      add(root, 1, n, dfn[x], dfn[y], v);
    };
    auto Q = [&](int x) -> pair<int, int> {
      return query(root, 1, n, dfn[x], dfn[x]);
    };
    const int INF = INT_MAX / 2;
    for (int i = 1; i <= n; i++) {
      path_add(i, i, INF);
    }
    int ans = 0;
    function<void(int)> dfs0 = [&](int cur) -> void {
      auto t = Q(cur);
      assert(t.second == 1);
      path_add(cur, cur, -t.first);
      path_add(1, cur, 1);
      assert(root->v.first >= 1);
      if (root->v.first == 1) {
        ans = max(ans, root->v.second);
      }
      for (auto nx: G1[cur]) {
        dfs0(nx);
      }
      path_add(1, cur, -1);
      path_add(cur, cur, INF);
    };
    dfs0(1);
    cout << ans << "\n";
  }
  return 0;
}
