#ACM0061. M. 王国(kingdom)

M. 王国(kingdom)

Legend

Xiao Chang and Xiao Ming live in a tree with nn nodes numbered from 11 to nn . 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 qq queries, given uu and vv, assuming that Xiao Chang lives in uu and Xiao Ming lives in vv, how many kingdoms will Xiao Chang visit on his way to visit Xiao Ming?

Input format

The first line contains a positive integer nn, indicating the number of nodes in the tree.

The next n1n - 1 lines, each containing two positive integers x,yx, y, indicate the existence of an edge between node xx and node yy.

The n+1n + 1th line contains a positive integer qq, indicating the number of queries.

The next qq lines, each containing two positive integers u,vu, v, describe a single query.

4n2×1054\leq n \leq 2\times 10^5 1q2×1051\leq q \leq 2\times 10^5 1x,yn1\leq x,y \leq n 1u,vn1\leq u,v \leq n

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