Under construction

Still being built. Things may change

Training Selection

Time limit1sMemory limit512 MB

Summary
Find the shortest index range [L, R] that contains at least one level-k subordinate for each request in a company tree.
Level

Medium7 of 10

Topics
Tree, DFS, Binary search, Two pointers
Solved
No attempts yet

Selecting the range

The employees of a company form a hierarchy. The company has nn employees, numbered from 1 to nn. The director has number 1. Every employee except the director has exactly one direct supervisor. The direct supervisor of employee ii has number pip_i, and pi<ip_i < i.

Employee xx is a subordinate of level 1 of employee yy if px=yp_x = y. For k>1k > 1, employee xx is a subordinate of level kk of employee yy if employee pxp_x is a subordinate of level k−1k-1 of employee yy.

The director wants to send some employees to a training course. He chooses two numbers LL and RR and sends every employee ii with L≤i≤RL \le i \le R.

Before choosing LL and RR, the director received mm requests. The jj-th request is a pair uju_j, kjk_j and means that employee uju_j asks to send one of their subordinates of level kjk_j to the course. To save money, the director wants the number of employees sent to the course to be as small as possible, while all requests are fulfilled. If several pairs (L,R)(L, R) are optimal, he chooses the one with the smallest LL.

Input

The first line contains nn, the number of employees (2≤n≤2000002 \le n \le 200000). The second line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi<i1 \le p_i < i), the direct supervisor of each employee ii. The third line contains mm, the number of requests. Each of the next mm lines contains two integers uju_j and kjk_j (1≤uj<n1 \le u_j < n, 1≤kj<n1 \le k_j < n). It is guaranteed that employee uju_j has at least one subordinate of level kjk_j.

Output

Print two integers LL and RR. If several pairs (L,R)(L, R) are optimal, print the pair with the smallest LL.

Hint

Employees 3, 4, 5, and 6 are sent to the course. Employee 3 is a subordinate of level 1 of employee 1. Employee 4 is a subordinate of level 2 of employee 1. Employee 6 is a subordinate of level 1 of employee 3.

Examples1

  1. Example 1

    Input
    7
    1 1 2 2 3 3
    3
    1 1
    3 1
    1 2
    
    Expected output
    3 6