CSES - Shared codeLink to this code: https://cses.fi/paste/f2a12df5ba1948a496b5bf/
#include<iostream>
#include<assert.h>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>

#define lli long long int
#define MOD 1000000007

using namespace std;

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int lcm(int a,int b){
	return (a*b)/gcd(a,b);
}

void solve(){
	int n,q;
	cin >> n >> q;
	vector<vector<int>> A(n,vector<int>(35));

	for(int i=0;i<n;i++){
		int t; cin >> t;t--;
		A[i][0]=t;
	}
	for(int j=1;j<35;j++){
		for(int i=0;i<n;i++){
			A[i][j] = A[ A[i][j-1] ][ j-1 ];
		}
	}
	/*for(int i=0;i<n;i++){
		for(int j=0;j<5;j++){
			cout << A[i][j] << " ";
		}
		cout << endl;
	}*/
	while(q--){
		lli u,k;
		cin >> u >> k;
		u--;
		lli ans = u;
		lli p = 35;
		lli num = pow(2,35);
		while(k>0){
			while(num>k){
				num = num/2;
				p--;
			}
			//cout << p << " " << num << endl;
			assert(p>=0);
			ans = A[ans][p];
			k -= num;
		}
		cout << ans+1 << endl;
	}

}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int testCase=1;
	while(testCase--){
		solve();
	}
	return 0;
}