#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
//raspund online cu ajutorul RMQ
int n,m,i,j,mbr,str,dp[300005][25],father[250005];
//formez dp de rmq
int main()
{
fin>>n>>m;
int cm=m;
//citesc arbore de tati
for(i=1;i<=n;i++)
{
fin>>j;
dp[i][0]=j;
}
for(i = 1; i < 20; i++)
{
for(j = 1; j <= n; j++)
{
dp[j][i] = dp[dp[j][i - 1]][i - 1];
}
}
//queries
while(cm--)
{
fin>>mbr>>str;
int x;
while(str > 0)
{
int put = log2(str); //2 //1 //0
x = dp[mbr][put]; //dp[9][2]=5 sa zicm
//x = dp[5][1] = 3 szcm //dp[3][0] = 2
str-=(1<<put); //7-4=3 //3-2=1 //1-1=0
mbr = x; //caut al 3-lea stramos al lui 5//acm devine 3
}
fout<<x<<'\n';
}
return 0;
}