#BS0000061. Tổ tiên của tôi (My Ancestor)

Tổ tiên của tôi (My Ancestor)

My Ancestor

Source: Thailand ICPC National

Version: Phuoc Hung OJ Extended

Problem Statement

A rooted family tree has root vertex 11. Each vertex vv has a positive value wvw_v. For every parent-child edge p→vp\to v, wp<wvw_p<w_v, so values are strictly increasing on every root-to-vertex path.

For each query (v,P)(v,P), find the proper ancestor of vv that is closest to the root and whose value is at least PP. Print −1-1 if no such ancestor exists. The PHOJ version fixes root 11 and explicitly lists the parent of every vertex 2,…,n2,\ldots,n.

Input

  • The first line contains n,qn,q.
  • The second line contains w1,…,wnw_1,\ldots,w_n.
  • The third line contains parents p2,…,pnp_2,\ldots,p_n, with 1≤pi<i1\le p_i<i.
  • Each of the next qq lines contains v,Pv,P.

Output

For each query, print the required ancestor index or -1.

Subtasks

  • Subtask 1 — 20%: n,q≤200n,q\le200, wi≤106w_i\le10^6.
  • Subtask 2 — 30%: n≤5000n\le5000, q≤5000q\le5000, wi≤109w_i\le10^9.
  • Subtask 3 — 50%: n≤80000n\le80000, q≤20000q\le20000, wi≤109w_i\le10^9, and wpi<wiw_{p_i}<w_i.

Example

Input

7 4
3 5 8 7 10 9 12
1 1 2 2 4 4
5 4
5 7
7 8
2 6

Output

2
-1
-1
-1

Explanation

For vertex 55, proper ancestors have values 3,53,5; threshold 44 selects vertex 22. For vertex 77, no proper ancestor reaches threshold 88.