#P1632E2. Distance Tree (hard version)
Distance Tree (hard version)
No testdata at current.
Description
This version of the problem differs from the previous one only in the constraint on .
A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them.
You are given a weighted tree with vertices, each edge has a weight of . Denote as the distance between vertex and vertex .
Let be the minimum possible value of if you can temporarily add an edge with weight between any two vertices and . Note that after this operation, the graph is no longer a tree.
For each integer from to , find .
The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer ().
Each of the next lines contains two integers and () indicating that there is an edge between vertices and . It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of over all test cases doesn't exceed .
For each test case, print integers in a single line, -th of which is equal to for all from to .
Input
The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer ().
Each of the next lines contains two integers and () indicating that there is an edge between vertices and . It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of over all test cases doesn't exceed .
Output
For each test case, print integers in a single line, -th of which is equal to for all from to .
Note
- For , we can an edge between vertices and , then and , so .
- For , no matter which edge we add, , and , so .