CSES - Shared codeLink to this code: https://cses.fi/paste/cb0b4f957c1102ae2bd1f5/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
ll mod =  1e9 + 7;
#define f first
#define ss second
#define all(x) x.begin(), x.end()
#define sz(x) x.size()

// binary lifting seekh le bro
const int MAX = 2e5 + 5;
int up[MAX][31];

void test_cases() {
    int n, q;
    int a, k;
    cin >> n >> q;

    for(int  i = 1; i <= n ; i++) {
        cin >> up[i][0];
    }

    for(int j = 1; j < 31; j++) {
        for(int i = 1; i <= n; i++) {
            up[i][j] = up[up[i][j - 1]][j - 1];
        }
    }
    // 183772 129965406
    while(q--) {
        cin >> a >> k;

        int L = 30;

        while(L >= 0) {
            if((1 << L) & k) {
                k ^= (1 << L);
                a = up[a][L];
            }
            L--;
        }
        cout << a << "\n";
    }
}

int main() {
    //    freopen("test_input.txt", "r", stdin);
//    freopen("asd.out", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int  t = 1;
    // cin>>t;

    while(t--) {

        test_cases();
        cout << "\n";
    }
}