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 = 'import sys\r\n\r\n \r\ndef eprint(*args):\r\n    print(*args, file=sys.stderr)\r\nzz=1\r\n \r\nsys.setrecursionlimit(10**6)\r\nif zz:\r\n\tinput=sys.stdin.readline\r\nelse:\t\r\n\tsys.stdin=open(\'input.txt\', \'r\')\r\n\tsys.stdout=open(\'all.txt\',\'w\')\r\ndi=[[-1,0],[1,0],[0,1],[0,-1]]\r\n\r\ndef string(s):\r\n\treturn "".join(s)\r\ndef fori(n):\r\n\treturn [fi() for i in range(n)]\t\r\ndef inc(d,c,x=1):\r\n\td[c]=d[c]+x if c in d else x\r\ndef bo(i):\r\n\treturn ord(i)-ord(\'A\')\t\r\ndef li():\r\n\treturn [int(xx) for xx in input().split()]\r\ndef fli():\r\n\treturn [float(x) for x in input().split()]\t\r\ndef comp(a,b):\r\n\tif(a>b):\r\n\t\treturn 2\r\n\treturn 2 if a==b else 0\t\t\r\ndef gi():\t\r\n\treturn [xx for xx in input().split()]\r\ndef cil(n,m):\r\n\treturn n//m+int(n%m>0)\t\r\ndef fi():\r\n\treturn int(input())\r\ndef pro(a): \r\n\treturn reduce(lambda a,b:a*b,a)\t\t\r\ndef swap(a,i,j): \r\n\ta[i],a[j]=a[j],a[i]\t\r\ndef si():\r\n\treturn list(input().rstrip())\t\r\ndef mi():\r\n\treturn \tmap(int,input().split())\t\t\t\r\ndef gh():\r\n\tsys.stdout.flush()\r\ndef isvalid(i,j,n,m):\r\n\treturn 0<=i<n and 0<=j<m \r\ndef bo(i):\r\n\treturn ord(i)-ord(\'a\')\t\r\ndef graph(n,m):\r\n\tfor i in range(m):\r\n\t\tx,y=mi()\r\n\t\ta[x].append(y)\r\n\t\ta[y].append(x)\r\n\r\n\r\n\r\nt=fi()\r\n\t\t\t\t\t\t\t \t\t\r\nwhile t>0:\r\n\tt-=1\r\n\tn=fi()\r\n\tp=li()\r\n\ta=[[] for i in range(n+1)]\r\n\tfor i in range(n-1):\r\n\t\ta[i+2].append(p[i])\r\n\t\ta[p[i]].append(i+2)\t\r\n\tsubtree=[1]*(n+1)\r\n\tdef dfs(i,par=-1):\r\n\t\tfor j in a[i]:\r\n\t\t\tif j!=par:\r\n\t\t\t\tdfs(j,i)\r\n\t\t\t\tsubtree[i]+=subtree[j]\t\t\t\r\n\tdfs(1,-1)\r\n\tans=[0]*(n+1)\r\n\t\r\n\tfor i in range(1,n+1):\r\n\t\tif i==1:\r\n\t\t\tans[i]=n\r\n\t\telse:\r\n\t\t\tans[i]+=subtree[i]+ans[p[i-2]]\t\r\n\tprint(max(ans))\t'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 17017