Trees of Tranquillity
Soroush and Keshi each have a labeled and rooted tree on $$$n$$$ vertices. Both of their trees are rooted from vertex $$$1$$$.Soroush and Keshi used to be at war. After endless decades of fighting, they finally became allies to prepare a Codeforces round. To celebrate this fortunate event, they decided to make a memorial graph on $$$n$$$ vertices.They add an edge between vertices $$$u$$$ and $$$v$$$ in the memorial graph if both of the following conditions hold:   One of $$$u$$$ or $$$v$$$ is the ancestor of the other in Soroush's tree.  Neither of $$$u$$$ or $$$v$$$ is the ancestor of the other in Keshi's tree. Here vertex $$$u$$$ is considered ancestor of vertex $$$v$$$, if $$$u$$$ lies on the path from $$$1$$$ (the root) to the $$$v$$$.Popping out of nowhere, Mashtali tried to find the maximum clique in the memorial graph for no reason. He failed because the graph was too big. Help Mashtali by finding the size of the maximum clique in the memorial graph.As a reminder, clique is a subset of vertices of the graph, each two of which are connected by an edge.
In the first and third test cases, you can pick any vertex.In the second test case, one of the maximum cliques is $$$\{2, 3, 4, 5\}$$$.In the fourth test case, one of the maximum cliques is $$$\{3, 4, 6\}$$$.
The first line contains an integer $$$t$$$ $$$(1\le t\le 3 \cdot 10^5)$$$ — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer $$$n$$$ $$$(2\le n\le 3 \cdot 10^5)$$$.The second line of each test case contains $$$n-1$$$ integers $$$a_2, \ldots, a_n$$$ $$$(1 \le a_i < i)$$$, $$$a_i$$$ being the parent of the vertex $$$i$$$ in Soroush's tree.The third line of each test case contains $$$n-1$$$ integers $$$b_2, \ldots, b_n$$$ $$$(1 \le b_i < i)$$$, $$$b_i$$$ being the parent of the vertex $$$i$$$ in Keshi's tree.It is guaranteed that the given graphs are trees.It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$.
For each test case print a single integer — the size of the maximum clique in the memorial graph.
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def euler_tour(G):
    s = []
    s.append(1)
    now = [0] * (n + 1)
    t = 1
    (t1, t2) = ([-1] * (n + 1), [-1] * (n + 1))
    v = []
    while s:
        i = s[-1]
        if t1[i] == -1:
            v.append(i)
            t1[i] = t
            t += 1
        if now[i] == len(G[i]):
            t2[i] = t
            t += 1
            s.pop()
        else:
            s.append(G[i][now[i]])
            now[i] += 1
    return (t1, t2, v)

def segment_tree(n):
    i = 2
    while True:
        if i >= n * 2:
            tree = [0] * i
            break
        else:
            i *= 2
    return tree

def update(i, x):
    i += len(tree) // 2
    tree[i] = x
    i //= 2
    while True:
        if i == 0:
            break
        tree[i] = max(tree[2 * i], tree[2 * i + 1])
        i //= 2
    return

def get_max(s, t):
    s += len(tree) // 2
    t += len(tree) // 2
    ans = 0
    while s <= t:
        if s % 2 == 0:
            s //= 2
        else:
            ans = max(ans, tree[s])
            s = (s + 1) // 2
        if t % 2 == 1:
            t //= 2
        else:
            ans = max(ans, tree[t])
            t = (t - 1) // 2
    return ans
t = int(input())
ans = []
for _ in range(t):
    n = int(input())
    a = [0] * 2 + list(map(int, input().split()))
    b = [0] * 2 + list(map(int, input().split()))
    G = [[] for _ in range(n + 1)]
    for i in range(2, n + 1):
        G[b[i]].append(i)
    (t1, t2, _) = euler_tour(G)
    t0 = [0] * (2 * n + 5)
    for i in range(1, n + 1):
        t0[t2[i]] = i
    G = [[] for _ in range(n + 1)]
    for i in range(2, n + 1):
        G[a[i]].append(i)
    st = [1]
    ans0 = 1
    now = 1
    tree = segment_tree(2 * n + 5)
    update(1, t2[1])
    z = [0] * (n + 1)
    while st:
        i = st[-1]
        if not len(G[i]):
            st.pop()
            update(t1[i], 0)
            j = z[i]
            if j:
                update(t1[j], t2[j])
            else:
                now -= 1
            continue
        j = G[i].pop()
        st.append(j)
        ma = get_max(1, t1[j] - 1)
        update(t1[j], t2[j])
        if ma < t1[j]:
            now += 1
        else:
            k = t0[ma]
            z[j] = k
            update(t1[k], 0)
        ans0 = max(ans0, now)
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))
import sys
input = sys.stdin.buffer.readline

class bit:

    def __init__(self, n):
        self.n = n
        self.a = [0] * (n + 1)

    def add(self, idx, val):
        idx += 1
        while idx < self.n:
            self.a[idx] += val
            idx += idx & -idx

    def sumr(self, idx):
        idx += 1
        tot = 0
        while idx:
            tot += self.a[idx]
            idx -= idx & -idx
        return tot

    def sum(self, l, r):
        return self.sumr(r) - self.sumr(l - 1)

def add(i):
    bit2.add(tin[i], 1)
    bit1.add(tin[i], i)
    bit1.add(tout[i] + 1, -i)

def remove(i):
    bit2.add(tin[i], -1)
    bit1.add(tin[i], -i)
    bit1.add(tout[i] + 1, i)
for _ in range(int(input())):
    n = int(input())
    adj = [[[] for i in range(n + 1)] for j in range(2)]
    for k in range(2):
        a = list(map(int, input().split()))
        for i in range(2, n + 1):
            adj[k][a[i - 2]].append(i)
    ans = 0
    tin = [0] * (n + 1)
    tout = [0] * (n + 1)
    t = -1
    s = [1]
    visited = [0] * (n + 1)
    while s:
        c = s[-1]
        if not visited[c]:
            t += 1
            tin[c] = t
            visited[c] = 1
            for ne in adj[1][c]:
                if not visited[ne]:
                    s.append(ne)
        else:
            tout[c] = t
            s.pop()
    curr_mx = 0
    bit1 = bit(n + 1)
    bit2 = bit(n + 1)
    ops = []
    s = [1]
    visited = [0] * (n + 1)
    while s:
        c = s[-1]
        if not visited[c]:
            visited[c] = 1
            u = bit1.sumr(tin[c])
            if u:
                remove(u)
                add(c)
                ops.append([u, c])
            elif bit2.sum(tin[c], tout[c]):
                ops.append([0, 0])
            else:
                add(c)
                curr_mx += 1
                ops.append([0, c])
            ans = max(ans, curr_mx)
            for ne in adj[0][c]:
                if not visited[ne]:
                    s.append(ne)
        else:
            (u, v) = ops.pop()
            if u:
                add(u)
                curr_mx += 1
            if v:
                remove(v)
                curr_mx -= 1
            s.pop()
    print(ans)
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class FenwickTree:

    def __init__(self, x):
        """"""
        self.bit = x
        for i in range(len(x)):
            j = i | i + 1
            if j < len(x):
                x[j] += x[i]

    def update(self, idx, x):
        """"""
        while idx < len(self.bit):
            self.bit[idx] += x
            idx |= idx + 1

    def query(self, end):
        """"""
        end += 1
        x = 0
        while end:
            x += self.bit[end - 1]
            end &= end - 1
        return x

    def findkth(self, k):
        """"""
        idx = -1
        for d in reversed(range(len(self.bit).bit_length())):
            right_idx = idx + (1 << d)
            if right_idx < len(self.bit) and k >= self.bit[right_idx]:
                idx = right_idx
                k -= self.bit[idx]
        return idx + 1
g1 = []
g2 = []
st = []
ft = []

def dfs1(graph, start=0):
    n = len(graph)
    tim = 1
    visited = [False] * n
    stack = [start]
    while stack:
        start = stack[-1]
        if not visited[start]:
            st[start] = tim
            tim = tim + 1
            visited[start] = True
            for child in graph[start]:
                if not visited[child]:
                    stack.append(child)
        else:
            stack.pop()
            ft[start] = tim

def Do(v):
    global ans, cnt, mem
    fen.update(st[v], 1)
    if fen.query(ft[v] - 1) - fen.query(st[v]):
        mem += [[v, 0, 0]]
        return
    u = fen2.query(st[v])
    ok[v] = 1
    cnt += 1
    fen2.update(st[v], v - u)
    fen2.update(ft[v], u - v)
    mem += [[v, u, ok[u]]]
    cnt -= ok[u]
    ok[u] = 0
    return

def undo():
    global cnt, ans, mem
    [v, u, a] = mem[-1]
    if a != 0:
        ok[u] = 1
        cnt += 1
    cnt -= 1
    ok[v] = 0
    fen.update(st[v], -1)
    fen2.update(st[v], u - v)
    fen2.update(ft[v], v - u)
    mem.pop()
    return

def dfs(graph, start=0):
    global ans, cnt
    n = len(graph)
    visited = [False] * n
    stack = [start]
    while stack:
        start = stack[-1]
        if not visited[start]:
            Do(start)
            ans = max(ans, cnt)
            visited[start] = True
            for child in graph[start]:
                if not visited[child]:
                    stack.append(child)
        else:
            stack.pop()
            undo()

def solve():
    global n, g1, g2, V, st, ft, fen, fen2, mem, ok, cnt, ans
    n = int(input())
    if n == 1:
        print(1)
        exit(0)
    g1 = []
    g2 = []
    for i in range(n):
        g1.append([])
        g2.append([])
    V = list(map(int, input().split()))
    for i in range(1, n):
        v = V[i - 1] - 1
        g1[v].append(i)
    V = list(map(int, input().split()))
    for i in range(1, n):
        v = V[i - 1] - 1
        g2[v].append(i)
    st = [0] * (n + 100)
    ft = [0] * (n + 100)
    fen = FenwickTree([0] * (n + 3))
    fen2 = FenwickTree([0] * (n + 3))
    mem = []
    ok = [0] * (n + 100)
    cnt = 0
    ans = 0
    dfs1(g2)
    dfs(g1)
    print(ans)
q = int(input())
for i in range(q):
    solve()