Polycarp has come across a family tree that outlines the relationships among individuals, numbered from to . Each person in this tree can have at most one direct ancestor. Additionally, each person owns a horse of a specific type, where person has a horse of type t_x. Note that horse types may not be unique.
A person numbered is called a -ancestor of a person numbered if is a direct ancestor of .
For k > 1, a person numbered is a -ancestor of a person numbered if has a -ancestor, and is a (k-1)-ancestor of that -ancestor of .
The family tree is acyclic, meaning no person is their own ancestor, either directly or indirectly (i.e., no person is an -ancestor of themselves for any x > 0).
A person numbered is a -son of a person numbered if is a -ancestor of .
Polycarp is curious about the number and types of horses owned by people. He has noted down pairs of numbers v_i, k_i. Your task is to determine the horse value of the set of k_i-sons of the person numbered v_i for each pair v_i, k_i.
The horse value of a set of people p_1, p_2, \ldots, p_r (p_1 < p_2 < \ldots < p_r) is calculated as (t_{p_1} \rightarrow (t_{p_2} \rightarrow (\ldots \rightarrow t_{p_r}))). If the set is empty, the horse value is zero. The operation x \rightarrow y represents the bitwise THEN operation on two -bit numbers. Refer to the note for a detailed explanation of this operation.
The first line contains a single integer (1 \leq n \leq 10^5), representing the number of people in the tree. The following lines describe each person. The -th line contains two integers t_i and r_i (0 \leq t_i \leq 10^9, 0 \leq r_i \leq n), where t_i is the horse type of person , and r_i is the number of their direct ancestor, or if they have none.
The next line contains a single integer (1 \leq m \leq 10^5), the number of Polycarp's queries. The subsequent lines each contain a pair of integers v_i, k_i (1 \leq v_i, k_i \leq n).
It is guaranteed that the family connections do not form cycles.
Output integers, separated by spaces, corresponding to the answers for Polycarp's queries, in the order they appear in the input.
Example 1
The result of the bitwise THEN operation on two -bit numbers is a -bit number, where each bit is determined by the bitwise THEN operation on the corresponding bits of the operands. Specifically,
The truth table for the THEN operation on bits is as follows:
0 \rightarrow 0 = 1;
0 \rightarrow 1 = 1;
1 \rightarrow 0 = 0;
1 \rightarrow 1 = 1.