#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 mxx(x) max_element(x.begin(), x.end()) - x.begin()
#define mnn(x) min_element(x.begin(), x.end()) - x.begin()
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
// binary lifting seekh le bro
void test_cases(){
ll n,q;
ll a , k ;
cin>>n>>q;
vector<ll>arr(n);
for(ll i = 0; i < n ;i++ ){
cin>>arr[i];
}
vector<vector<ll>>up(n , vector<ll>(31, -1));
for(ll i=0;i<n;i++){
up[i][0] = arr[i]-1;
}
for(ll j = 1; j < 31; j++){
for(ll i = 0 ; i < n ;i++){
if(up[i][j-1] == -1){
up[i][j] = -1;
continue;
}
up[i][j] = up[up[i][j-1]][j-1];
}
}
// 183772 129965406
while(q--){
cin>>a>>k;
a--;
ll L = 30;
ll st = a;
while(L>=0){
if(1<<L <= k){
k-=(1<<L);
a = up[a][L];
if(a==-1){
a = st;
break;
}
}
L--;
}
cout<<a+1<<"\n";
}
}
int main(){
fast_io;
ll t=1;
// cin>>t;
while(t--){
test_cases();
cout<<"\n";
}
}
ICNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CiB1c2luZyBuYW1lc3BhY2Ugc3RkOwogI2RlZmluZSBsbCBsb25nIGxvbmcKICNkZWZpbmUgcGIgcHVzaF9iYWNrCiBsbCBtb2QgPSAgMWU5Kzc7CiAjZGVmaW5lIGYgZmlyc3QKICNkZWZpbmUgc3Mgc2Vjb25kCiAjZGVmaW5lIG14eCh4KSBtYXhfZWxlbWVudCh4LmJlZ2luKCksIHguZW5kKCkpIC0geC5iZWdpbigpCiAjZGVmaW5lIG1ubih4KSBtaW5fZWxlbWVudCh4LmJlZ2luKCksIHguZW5kKCkpIC0geC5iZWdpbigpCiAjZGVmaW5lIGZhc3RfaW8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7Y2luLnRpZShOVUxMKQogI2RlZmluZSBhbGwoeCkgeC5iZWdpbigpLCB4LmVuZCgpCiAjZGVmaW5lIHN6KHgpIHguc2l6ZSgpCiAKIC8vIGJpbmFyeSBsaWZ0aW5nIHNlZWtoIGxlIGJybwogCiB2b2lkIHRlc3RfY2FzZXMoKXsKIAogICAgIGxsIG4scTsKICAgICBsbCBhICwgayA7CiAgICAgY2luPj5uPj5xOwogCiAgICAgdmVjdG9yPGxsPmFycihuKTsKIAogICAgIGZvcihsbCBpID0gMDsgaSA8IG4gO2krKyApewogICAgICAgICBjaW4+PmFycltpXTsKICAgICB9CiAKICAgICB2ZWN0b3I8dmVjdG9yPGxsPj51cChuICwgdmVjdG9yPGxsPigzMSwgLTEpKTsKIAogICAgIGZvcihsbCBpPTA7aTxuO2krKyl7CiAgICAgICAgIHVwW2ldWzBdID0gYXJyW2ldLTE7CiAgICAgfQogCiAgICAgICAgIGZvcihsbCBqID0gMTsgaiA8IDMxOyBqKyspeyAKICAgICAgICAgICAgIGZvcihsbCBpID0gMCA7IGkgPCBuIDtpKyspewogCiAgICAgICAgICAgICBpZih1cFtpXVtqLTFdID09IC0xKXsKICAgICAgICAgICAgICAgICB1cFtpXVtqXSA9IC0xOwogICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgfQogCiAgICAgICAgICAgICB1cFtpXVtqXSA9IHVwW3VwW2ldW2otMV1dW2otMV07CiAgICAgICAgIH0KICAgICB9CiAgICAgLy8gMTgzNzcyIDEyOTk2NTQwNgogICAgIHdoaWxlKHEtLSl7CiAKICAgICAgICAgY2luPj5hPj5rOwogICAgICAgICBhLS07CiAKICAgICAgICAgbGwgTCA9IDMwOwogCiAgICAgICAgIGxsIHN0ID0gYTsKICAgICAgICAgd2hpbGUoTD49MCl7CiAgICAgICAgICAgICBpZigxPDxMIDw9IGspewogICAgICAgICAgICAgICAgIGstPSgxPDxMKTsKICAgICAgICAgICAgICAgICBhID0gdXBbYV1bTF07CiAgICAgICAgICAgICAgICAgaWYoYT09LTEpewogICAgICAgICAgICAgICAgICAgICBhID0gc3Q7CiAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgIH0KICAgICAgICAgICAgIEwtLTsKICAgICAgICAgfQogICAgICAgICBjb3V0PDxhKzE8PCJcbiI7CiAgICAgfQogfSAKIAogaW50IG1haW4oKXsKICAKICAgICBmYXN0X2lvOwogICAgIGxsIHQ9MTsKICAgICAvLyBjaW4+PnQ7CiAKICAgICB3aGlsZSh0LS0pewogICAgICAgCiAgICAgICAgIHRlc3RfY2FzZXMoKTsKICAgICAgICAgY291dDw8IlxuIjsKICAgICB9CiB9