#P7856. Tree

Tree

Tree

Problem Description

You are given a tree consisting of nn vertices numbered 11 to nn rooted at node 11. The parent of the ii-th vertices is pip_i. You can move from a vertex to any of its children. What's more, you can add one directed edge between any two different vertices, and you can move through this edge too. You need to maximize the number of pairs (x,y)(x,y) such that xx can move to yy through the edges after adding the edge. Note that xx can also move to xx.

Input

The first line contains one integer TT (1T100000)(1\leq T\leq 100000) — the number of test cases. The first line of each test case contains only one integer n(1n5×105)n(1\leq n\leq 5\times 10^5) — the number of vertices in the tree. The second line of each test case contains n1n-1 integers p2,p3,,pn(1pi<i)p_2,p_3,\dots,p_n (1\leq p_i<i) — the parent of each non-root node. The sum of nn over all test cases does not exceed 10610^6.

Output

Print TT integers — for each test case output the maximum number of pairs (x,y)(x,y) that vertices xx can move to yy after adding one edge.

Sample Input

2
5
1 1 2 2
6
1 2 3 1 3

Sample Output

17
26

Source

2020 Multi-University Training Contest 9