Lowest Common Ancestor

Given a rooted tree, answer each query with the number of the deepest vertex that is an ancestor of both given vertices.

Medium4TreeDFSInterviewNo attempts yetTime limit3sMemory limit256 MB

Problem

You are given a tree with NN vertices. The vertices are numbered from 1 to NN, and vertex 1 is the root.

The lowest common ancestor of two vertices uu and vv is the vertex that is an ancestor of both and lies farthest from the root. A vertex counts as an ancestor of itself here.

You are given MM pairs of vertices. For each pair, find the number of its lowest common ancestor.

Input

The first line contains the number of vertices NN (2N500002 \le N \le 50000).

Each of the next N1N-1 lines contains the numbers of two vertices joined by an edge. An edge is not guaranteed to be given in parent then child order.

The next line contains the number of queries MM (1M100001 \le M \le 10000). Each of the following MM lines contains one pair of vertices. The two vertices in a pair may be the same.

Output

Print MM lines. On the ii-th line print the number of the lowest common ancestor of the ii-th pair given in the input.