#P7856. Tree
Tree
Tree
Problem Description
You are given a tree consisting of vertices numbered to rooted at node . The parent of the -th vertices is . 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 such that can move to through the edges after adding the edge. Note that can also move to .
Input
The first line contains one integer — the number of test cases. The first line of each test case contains only one integer — the number of vertices in the tree. The second line of each test case contains integers — the parent of each non-root node. The sum of over all test cases does not exceed .
Output
Print integers — for each test case output the maximum number of pairs that vertices can move to 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