The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$.
Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node.
For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$.

-----Output-----
For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally.

-----Constraints-----
- $1 \le T \le 5$
- $2 \le N \le 10^5$
- $1 \le p_i < i$ for each valid $i$

-----Subtasks-----
Subtask #1 (100 points): original constraints

-----Example Input-----
2
3
1 1
5
1 1 2 2

-----Example Output-----
4
9

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '#================= author:------sushmanth ========================\r\n\r\nfrom sys import setrecursionlimit\r\nsetrecursionlimit(10**8)\r\n\r\ndef max_mex(dept):\r\n    \r\n    for i in at_dept[dept]:\r\n\r\n        m,l=0,0\r\n        for j in child[i]:\r\n            m=max(m,mex[j][1])\r\n            l+=mex[j][0]\r\n\r\n        mex[i]=[l+1,m+l+1]\r\n\r\n    if(dept > 1):\r\n        max_mex(dept-1)\r\n            \r\n\r\n\r\ndef all_dept(arr,dept=2):\r\n    for i in arr:\r\n        at_dept[dept].append(i)\r\n        all_dept(child[i],dept+1)\r\n\r\n\r\nfor T in range(int(input())):\r\n    n=int(input())\r\n    parent=list(map(int,input().split()))\r\n\r\n\r\n    child=[[] for i in range(n+1)]\r\n    for i in range(n-1):\r\n        \r\n        child[parent[i]].append(i+2)\r\n    \r\n    at_dept=[[] for i in range(n+1)]\r\n    at_dept[1].append(1)\r\n    all_dept(child[1])\r\n\r\n    for arr in at_dept[::-1]:\r\n        if(arr==[]):\r\n            at_dept.pop()\r\n        else:\r\n            break\r\n\r\n    mex=[0]*(n+1)\r\n    for i in at_dept[-1]:\r\n        mex[i]=[1,1]\r\n\r\n    max_mex(len(at_dept)-2)\r\n\r\n    print(mex[1][1])\r\n\r\n\r\n    \r\n    \r\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 13807