#P7746. [2019年杭电多校]Rikka with Travels

[2019年杭电多校]Rikka with Travels

Rikka with Travels

Problem Description

To enjoy their summer vacations, Rikka and Yuta decide to go travels. According to past experiences, contradictions always arose when they were planning for the same trip. This time, they decide to make plans dividually and will go travel twice. Coincidentally, they choose the same country Fantasy as the destination, which is a small island country on the Pacific. There are nn cities in Fantasy and they are connected with n1n-1 two-way roads. It is guaranteed that any two cities can reach each other by the road system. Though Rikka and Yuta love travels, visiting the same city more than once is still boring for them. Therefore, both Rikka and Yuta choose a simple path (i.e., a path without visiting any city more than once) as her/his plan. Moreover, they want to ensure the two paths do not intersect on any city. Suppose Rikka chooses the path from aa to bb, Yuta chooses the path from cc to dd (both a=ba=b and c=dc=d are allowed), they define the feature of the plan is an ordered pair (L(a,b),L(c,d))(L(a,b),L(c,d)), where L(x,y)L(x,y) represents the number of cities on the path from xx to yy. Now, Rikka wants to count the number of different features, i.e., the number of different integer pairs (l1,l2)(l_1,l_2) which satisfies there exists a valid travel plan (a,b,c,d)(a,b,c,d) meets L(a,b)=l1,L(c,d)=l2L(a,b) = l_1, L(c,d) = l_2. Since Rikka and Yuta are busy with planning their trip, Rikka asks you to help her calculate the answer.

Input

The first line of the input contains a single integer T(1T300)T(1 \leq T \leq 300), the number of test cases. For each test case, the first line contains a single integer n(1n105)n(1 \leq n \leq 10^5), the number of cities in Fantasy. Then n1n-1 lines follow. Each line contains two integers ui,vi(1ui,vin)u_i,v_i(1 \leq u_i,v_i \leq n) which represents a two-way road (ui,vi)(u_i,v_i) in the road system. The input guarantees that there are no more than 55 test cases with n>500n > 500.

Output

For each test case, output a single line with a single integer, the answer. Hint In the first test case, the possible features are (1,1),(1,2),(1,3),(2,1),(3,1)(1,1), (1,2), (1,3), (2,1), (3,1). Therefore the answer is 55. In the second test case, the possible features are $(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(3,1),(3,2), (4,1)$. Therefore the answer is 1010.

Sample Input

2

4

1 2

1 3

1 4

5

1 2

2 3

3 4

3 5

Sample Output

5

10

Source

2019 Multi-University Training Contest 9