You are given a tree with vertices. Here, a tree is an undirected graph in which any two vertices are connected by exactly one simple path, or equivalently a connected acyclic undirected graph. A tree with vertices, contains exactly undirected edges.
You have to process queries. In each of the query, you will be given 3 positive integers , and . You have to perform the following operations on your tree —
Add an edge between and .
Remove an edge such that the resulting graph is still a tree. You can also remove the newly added edge.
Minimize the sum of distance of every vertex from the vertex . Formally , let denote the shortest distance from vertex to vertex . You have to minimize
Print the Minimized Sum
Note that, Each query is independent which means operations in one query does not affect the other queries.
The first line of the input will contain an integer, which denotes the number of vertices of the tree.
Each of the next lines will contain space-separated integers and , indicating an edge between vertices and . It is guaranteed that the given edges form a tree.
The next line will contain an integer , the number of queries.
Finally, each of the next line will contain integers , and — representing each query.
Print the answer for each query in separate lines.
Input | Output |
---|---|
5 2 1 3 1 4 3 5 1 4 4 2 1 2 1 2 4 2 1 4 4 1 | 9 8 9 6 |
Input | Output |
---|---|
6 1 2 2 3 3 4 4 6 1 5 2 1 5 6 1 5 4 | 9 9 |
Input | Output |
---|---|
10 2 1 3 2 4 3 5 3 6 4 7 2 8 4 9 8 10 7 2 1 10 4 9 1 9 | 27 23 |