#ds900. 树上LCA2

树上LCA2

Tree LCA 2

Problem Description

You are given a tree of nn nodes rooted at node 11; the nodes are numbered 11 to nn. There are mm queries; for each query (u,v)(u, v), output the lowest common ancestor of node uu and node vv.

Input Format

The first line contains one integer nn, the number of nodes.

The next n−1n - 1 lines each contain two integers x,yx, y, meaning there is an edge between node xx and node yy.

The input is guaranteed to form a tree.

The next line contains one integer mm, the number of queries.

The next mm lines each contain two integers u,vu, v, representing one query.

Output Format

Output mm lines in total; for each query output one line containing one integer: the index of the lowest common ancestor of the two given nodes.

Sample Input

4
1 2
1 3
3 4
2
1 2
2 4

Sample Output

1
1

Constraints

For all data, it is guaranteed that 1≤n,m≤100000, 1≤u,v≤n1 \le n, m \le 100000,\ 1 \le u, v \le n.