#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
const int MaxN=100005,MaxLog=17,MAX=131072;
using namespace std;
FILE *IN,*OUT;
int Father[MaxLog][MaxN],h[MaxN],N,M,X,Y,t;
int pos=0,out=0;
char f[MAX],Out[MAX],str[10];
inline void Read(int &nr)
{
nr=0;
while(!isdigit(f[pos]))
{
++pos;
if(pos==MAX)
fread(f,MAX,1,IN),pos=0;
}
while(isdigit(f[pos]))
{
nr=10*nr+f[pos++]-'0';
if(pos==MAX)
fread(f,MAX,1,IN),pos=0;
}
}
inline void Write_Ch(char ch)
{
Out[out++]=ch;
if(out==MAX)
fwrite(Out,MAX,1,OUT),out=0;
}
inline void Write_Int(int nr)
{
int x=0;
do
{
str[x++]=nr%10+'0';
nr/=10;
}
while(nr);
while(x>0)
Write_Ch(str[--x]);
}
int LCA(int a,int b)
{
if(h[a]<h[b])
{
t=a;
a=b;
b=t;
}
if(h[a]>h[b])
{
int i=MaxLog-1;
while(i>=0)
{
if(h[Father[i][a]]>=h[b])
a=Father[i][a];
i--;
}
}
if(a==b)return a;
else
{
int i=MaxLog-1;
while(i>=0)
{
if(Father[i][a]!=Father[i][b])
a=Father[i][a],b=Father[i][b];
i--;
}
}
return Father[0][a];
}
int main()
{
IN=fopen("lca.in","r");
OUT=fopen("lca.out","w");
fread(f,1,MAX,IN);
Read(N),Read(M);
Father[0][1]=-1;
h[1]=1;
for(int i=2;i<=N;i++)
{
Read(Father[0][i]);
h[i]=1+h[Father[0][i]];
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<MaxLog;j++)
Father[j][i]=Father[j-1][Father[j-1][i]];
}
for(int i=1;i<=M;i++)
{
Read(X),Read(Y);
Write_Int(LCA(X,Y));
Write_Ch('\n');
}
if(out>0)fwrite(Out,1,out,OUT);
return 0;
}