PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000017. Nhảy trên hành tinh (Planets Queries I)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nhảy trên hành tinh (Planets Queries I)

Planets Queries I

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn planets numbered 11 to nn. Planet uu has exactly one teleporter leading to fuf_u, possibly back to itself.

For each query (x,k)(x,k), start at planet xx, follow teleporters exactly kk times, and report the final planet.

Planets can be revisited when teleporters form a cycle.

Input

  • The first line contains n,qn,q.
  • The second line contains nn integers f1,…,fnf_1,\ldots,f_n.
  • The next qq lines contain x,kx,k.

Output

Print the destination planet index on its own line for each query.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; q≤60q\le60; 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 1≤fi,x≤n1\le f_i,x\le n; 0≤k≤1090\le k\le10^9.
  • Subtask 2 (30 points): n≤1000n\le1000; q≤3000q\le3000; 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 1≤fi,x≤n1\le f_i,x\le n; 0≤k≤1090\le k\le10^9.
  • Subtask 3 (50 points): 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; 1≤fi,x≤n1\le f_i,x\le n; 0≤k≤1090\le k\le10^9.

Examples

Input

4 3
2 1 1 4
1 2
3 4
4 1

Output

1
2
4

Explanation

From planet 11, two teleports follow 1o2o11 o2 o1, returning to 11. Four teleports from planet 33 end at planet 22.