// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
 
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#endif
 
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
 
 
#ifdef __GNUC__
#define _packed __attribute__((packed))
#else
#define _packed
#endif
 
long double EPS = 0.000000001;
 
using namespace std;
 
using ll = long long;
 
const ll mod = 998244353;
using ll = long long;
using pp = pair<ll, ll>;
 
int gcd (int a, int b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
 
int n;
vector<int> g1[300000];
vector<int> g2[300000];
 
pp a[300000];
pp err[300000];
 
int res = 0;
 
struct Zal {
    void insert(int v) {
        auto it = s.upper_bound(a[v]);
        if (it != s.end() && a[v].second > it->first) {
            return;
        }
        if (it != s.begin()) {
            --it;
            if (it->second > a[v].first) {
                err[v] = *it;
                s.erase(it);
            }
        }
        s.insert(a[v]);
    }
 
    void erase(int v) {
        s.erase(a[v]);
        if (err[v].first != -1) {
        	s.insert(err[v]);
        }
    }
 
    int get() {
        return s.size();
    }
    void clear() {
        s.clear();
        
    }
    set<pp> s;
};
 
int cnt = 0;
Zal zal;
void precalc(int v) {
    a[v].first = cnt;
    ++cnt;
    for (auto el : g2[v]) {
        precalc(el);
    }
    a[v].second = cnt;
    ++cnt;
}
 
void dfs(int v) {
    zal.insert(v);
    bool leaf = true;
    for (auto el : g1[v]) {
        leaf = false;
        dfs(el);
    }
    if (leaf) {
        res = max(res, zal.get());
    }
    zal.erase(v);
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("hui.in", "r", stdin);
    //freopen("hui.out", "w", stdin);
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        res = 0;
        cnt = 0;
        zal.clear();
        for (int i = 0; i < n; ++i) {
            err[i] = {-1, -1};
        }
        for (int i = 0; i < n; ++i) {
            g1[i].clear();
            g2[i].clear();
        }
        for (int i = 1; i < n; ++i) {
            int cur;
            cin >> cur;
            g1[--cur].push_back(i);
        }
        for (int i = 1; i < n; ++i) {
            int cur;
            cin >> cur;
            g2[--cur].push_back(i);
        }
        precalc(0);
        dfs(0);
        cout << res << endl;
    }
    return 0;
}