#Q137. 「一本通 4.4 练习 2」祖孙询问

「一本通 4.4 练习 2」祖孙询问

Description

Given a rooted tree with nn nodes. There are mm queries, each query provides a pair of node numbers xx and yy, asking about the ancestor-descendant relationship between xx and yy.

Input Format

The first line of input contains an integer nn representing the number of nodes;

The next nn lines each contain a pair of integers aa and bb indicating there is an edge between aa and bb. If bb is 1-1, then aa is the root of the tree;

The (n+2)(n+2)-th line is an integer mm representing the number of queries;

The next mm lines each contain two positive integers xx and yy, representing a query.

Output Format

For each query, if xx is an ancestor of yy, output 11; if yy is an ancestor of xx, output 22; otherwise, output 00.

Sample 1

10
234 -1
12 234
13 234
14 234
15 234
16 234
17 234
18 234
19 234
233 19
5
234 233
233 12
233 13
233 15
233 19

1
0
0
0
2

Data Range and Hints

For 30%30\% of the data, 1n,m1031\le n,m\le 10^3;

For 100%100\% of the data, 1n,m4×1041\le n,m\le 4\times 10^4, and each node's number does not exceed 4×1044\times 10^4.