PYRA - Treeramids


Daniel is building towers. He has a big amount of block of the size 1x1xK (for any K). Out of them he is building towers according to the following rules: the longest block is put in the basement of the tower. Several towers built by the same rules can be put on this block. The distance between the bases of those towers should be 1 and the distance between the bases of the towers and the edges of the common basement should be 1. There should be a block of the length 1 on the top of each tower. Daniel encodes every tower with root tree. For example:

The root of the tree corresponds to the basement of the tower. Its descendants correspond to the towers standing on it. The leaves correspond to the blocks of the size 1x1x1 on the tops. Given the root tree describing one of those structures, calculate the total volume of the structure.

Input

The first line of input contains the number t - the number of tests. Next comes the description of t tests. Each test starts with an integer n - the number of nodes of the tree. This is followed by n-1 line, consisting of two integers a and b - number of nodes connected by an edge. Nodes are numbered from 0 to n-1. The root of the tree will always be a node with number 0. Otherwise the nodes and edges can go in any order.

Constraints

1 <= t <= 25
1 <= n <= 10^4

Output

For each test case print the total volume of the structure build according to the rules in the problem statement.

Example

Input:
2
7
0 1
2 0
0 3
2 4
5 2
6 3
3
1 2
1 0

Output:
25
9

hide comments
Shaka Shadows: 2011-09-06 04:10:43

@Answer. RE: No long long is not required at all.

Eternal Answer: 2011-09-05 02:53:58

Is the answer exceed long long?

darryl: 2011-08-06 16:40:34

Just a simple DFS indeed

Santiago Zubieta: 2011-05-30 10:18:47

in the pair input does it matter if 0 goes first and then 3 or if 3 goes first and then 0 or it doesn't matters?

Santiago Palacio: 2011-05-29 06:14:51

hint: you can always choose 0 as the root.

Last edit: 2011-05-29 06:40:10
Spooky: 2011-04-01 18:18:41

yes

Gurpreet Singh: 2011-03-31 17:24:27

for n=1...
Answer should be 1 ???

Spooky: 2010-12-29 11:41:43

yeah... pretty much...

[Retired] Fendy Kosnatha: 2010-12-25 13:58:48

it's just simple DFS right?

Ninjaflyte: 2010-06-15 07:04:48

Is there any problem with the input or the limits? My prog seems to work fine here but gives WA on the judge.


Added by:Spooky
Date:2010-03-09
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 SQLITE VB.NET
Resource:Advancement Spring 2010, http://sevolymp.uuuq.com/