#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>
#include <vector>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
#define in fin
#define out fout
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int ans[250001][20];
int f[250001];
signed main(){
ios_base::sync_with_stdio(false);
in.tie(NULL);
int n, m; in >> n >> m;
for(int i = 1; i <= n; i++) in >> f[i];
for(int j = 1; j <= n; j++){
ans[j][0] = f[j];
}
for(int j = 1; j < 20; j++){
for(int i = 1; i <= n; i++){
ans[i][j] = ans[ ans[i][j - 1] ][j - 1];
}
}
// cout << "ans : \n";
// for(int i = 1; i <= n; i++){
// for(int j = 0; j <= 4; j++) cout << ans[i][j] << " ";
// cout << "\n";
// }
for(int i = 0; i < m; i++){
int p, q; in >> p >> q;
while(q > 0){
int l = log2(q);
p = ans[p][l];
q -= (1 << l);
}
out << p << '\n';
}
return 0;
}