#ACM0061. M. 王国(kingdom)
M. 王国(kingdom)
Legend
Xiao Chang and Xiao Ming live in a tree with nodes numbered from to . There are a number of kingdoms in the tree.
If a node has more than two edges, then a kingdom appears at that node with the same number as the node (at least one such node is guaranteed to exist). The capital of the kingdom is this node. The kingdom expands its territory as much as possible on a ``first come, first served'' basis. In other words, an unoccupied node will be occupied by the kingdom whose capital is the closest to the node (if there is more than one, the one with the smallest number).
Xiao Chang and Xiao Ming are friends, so Xiao Chang will often visit Xiao Ming, and on the way, he may pass the territories of several kingdoms. Also, to save time, Xiao Chang will follow the shortest path to reach Xiao Ming's home. Now you need to answer queries, given and , assuming that Xiao Chang lives in and Xiao Ming lives in , how many kingdoms will Xiao Chang visit on his way to visit Xiao Ming?
Input format
The first line contains a positive integer , indicating the number of nodes in the tree.
The next lines, each containing two positive integers , indicate the existence of an edge between node and node .
The th line contains a positive integer , indicating the number of queries.
The next lines, each containing two positive integers , describe a single query.
Output format
For each query, output a line containing an integer indicating the answer.
6
1 10 10
1 10 -10
1 10 0
2 30
3 30
3 20
120
150
0