#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, m, i, p, a, b, rez[300005], start[300005], x;
vector <int> G[250005];
vector <pair <int, int>> k[300005];
vector <int> Q;
void dfs(int nod){
if(!k[nod].empty()){
for(auto itr: k[nod]){
if(p-itr.first>=1)
rez[itr.second]=Q[p-itr.first-1];
else
rez[itr.second]=0;
}
}
for(auto it: G[nod]){
Q.push_back(it);
p++;
dfs(it);
Q.pop_back();
p--;
}
}
int main()
{
fin>>n>>m;
for(i=1; i<=n; ++i){
fin>>x;
G[x].push_back(i);
}
for(i=1; i<=m; ++i){
fin>>a>>b;
k[a].push_back(make_pair(b,i));
}
p=0;
dfs(0);
for(i=1; i<=m; ++i)
cout<<rez[i]<<'\n';
return 0;
}