#ds900. 树上LCA2
树上LCA2
Tree LCA 2
Problem Description
You are given a tree of nodes rooted at node ; the nodes are numbered to . There are queries; for each query , output the lowest common ancestor of node and node .
Input Format
The first line contains one integer , the number of nodes.
The next lines each contain two integers , meaning there is an edge between node and node .
The input is guaranteed to form a tree.
The next line contains one integer , the number of queries.
The next lines each contain two integers , representing one query.
Output Format
Output 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 .