Yousef is given a tree$$$^{\text{∗}}$$$ of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$.
Let $$$S$$$ be the set of all leaves$$$^{\text{†}}$$$ of the given tree (this set is determined from the original tree and does not change).
Yousef repeats the following process until the number of unchosen vertices is at most one:
Your task is to help Yousef determine the minimum possible total cost achievable after the process ends.
$$$^{\text{∗}}$$$A tree is an undirected connected graph in which there are no cycles.
$$$^{\text{†}}$$$A leaf of a tree is a vertex that is connected to at most one other vertex.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow.
Each test case consists of several lines. The first line of the test case contains an integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of vertices in the tree.
Then $$$n − 1$$$ lines follow, each of which contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) that describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer — the minimum possible total cost achievable after the process ends.
441 22 32 461 22 32 44 54 671 22 33 42 55 65 751 21 33 43 5
2452
In the first test case, the set of leaves $$$S$$$ contains $$$\{1, 3, 4\}$$$. Yousef can do the following:
Since there is one vertex left unchosen, the process stops, and the total cost is $$$2$$$. It can be shown that $$$2$$$ is the minimum answer.
The given tree in the first test case. In the second test case, the set of leaves $$$S$$$ contains $$$\{1, 3, 5, 6\}$$$. Yousef can do the following:
Since there is no vertex left unchosen, the process stops, and the total cost is $$$4$$$.
The given tree in the second test case.