#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, m;
int ancestor[250005][19];
int main() {
int p, q;
fin >> n >> m;
for(int i=1; i <= n; i++)
fin >> ancestor[i][0];
for(int i = 1; i <= n; i++)
for(int j = 1; j <=18; j++)
ancestor[i][j] = ancestor[ancestor[i][j-1]][j-1];
for(int i = 0; i < m; i++) {
fin >> q >> p;
for(int j = 18; j >= 0; j--){
if(p & (1 << j)) {
q = ancestor[q][j];
if(q == 0) break;
}
}
fout << q << "\n";
}
return 0;
}