#include<fstream>
#include<vector>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int a[250001],niv[250001],n,m,x,p,nr;
vector<int> nivel[250001];
vector<int> q[250001];
struct point{
int x,max1;
}b[250001];
int dfs(int nod,int lvl){
nivel[lvl].push_back(nod);
niv[nod]=lvl;
nr++;
b[nod].x=nr;
b[nod].max1=nr;
for(int i=0;i<q[nod].size();i++){
b[nod].max1=max(b[nod].max1,dfs(q[nod][i],lvl+1));
}
return b[nod].max1;
}
int stramos(int x,int p){
if(niv[x]-p<=0) return 0;
for(int i=0;i<nivel[niv[x]-p].size();i++){
if(b[nivel[niv[x]-p][i]].x<=b[x].x&&b[x].x<=b[nivel[niv[x]-p][i]].max1) return nivel[niv[x]-p][i];
}
}
int main(){
f>>n>>m;
for(int i=1;i<=n;i++){
f>>x;
q[x].push_back(i);
}
nr=0;
dfs(0,1);
while(m--){
f>>x>>p;
g<<stramos(x,p)<<"\n";
}
return 0;
}