You are given a tree with nodes indexed from to , rooted in node . Each node has a value .
Giorgio asked you to answer queries of the form : Which is the -th largest value among those of the nodes on the path between nodes and (including endpoints)?
Here, corresponds to the largest value, to the second largest, etc.
Input data
The first line contains the integer : the number of nodes in the tree.
The second line contains integers : the -th integer represents the value of node .
The third line contains integers : the -th integer represents , the parent of node .
The fourth line contains a single integer : the number of queries.
The following lines contain integers each, representing a query.
Output data
You have to output lines, the -th of them containing a single integer: the answer to the -th query.
Constraints and Clarifications
- .
- for each .
- for each .
- .
- for each .
- for each .
# | Score | Restrictions |
---|---|---|
1 | 0 | Examples. |
2 | 8 | |
3 | 11 | , for each |
4 | 18 | |
5 | 13 | , for each } |
6 | 20 | |
7 | 12 | for each |
8 | 18 | No additional limitations. |
Example 1
stdin
6
3 6 6 6 6 6
5 1 1 1 1
5
4 2 2
2 4 2
1 1 1
4 3 2
4 3 1
stdout
6
6
3
6
6
Explanation
In the first sample case:
- In the first query, nodes are on the path between and , their values are, respectively, . The second largest of them is .
- In the second query, nodes are on the path between and , their values are, respectively, . The second largest of them is .
- In the third query, only node is on the path between and . Its value is , the largest value on the path is therefore .
- In the fourth query, nodes are on the path between and , their values are, respectively, . The second largest of them is .
- In the fifth query, nodes are on the path between and , their values are, respectively, . The second largest of them is .
Example 2
stdin
10
5 10 1 1 2 5 9 8 2 9
8 2 7 9 5 1 1 8 5
10
10 3 2
1 4 2
6 8 1
3 4 2
2 9 2
9 7 2
3 3 1
6 4 4
5 5 1
3 8 1
stdout
9
5
8
9
8
8
1
5
2
10