#include<stdio.h>
#include<vector>
#include<algorithm>
#define maxn 100005
#define pb push_back
#define mp make_pair
using namespace std;
FILE*f=fopen("lca.in","r");
FILE*g=fopen("lca.out","w");
int n,m,k,fictive,N,index,val,loc,a,b;
int viz[maxn],nivelnod[maxn],indT[maxn],height[maxn],lant[maxn],tata_lant[maxn];
int first[maxn],last[maxn],sigure;
double arb_prob[4*maxn],now,doubleval; int arb_sigure[4*maxn];
vector< pair< int,pair<int,double> > >G[maxn];
vector< pair< int,double> >up[maxn],down[maxn];
double sol;
void dfs ( int nod , int nivel ){
viz[nod] = 1;
nivelnod[nod] = nivel;
first[nod] = ++index;
height[nod] = 1;
int heavy = 0;
for ( vector< pair< int,pair<int,double> > >::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){
int vecin = itt->first;
if ( !viz[vecin] ){
indT[vecin] = itt->second.first;
dfs(vecin,nivel+1);
height[nod] += height[vecin];
tata_lant[lant[vecin]] = nod;
if ( height[vecin] > height[heavy] ){
heavy = vecin;
}
}
}
if ( height[nod] == 1 ){
lant[nod] = ++lant[0];
}
else{
lant[nod] = lant[heavy];
}
last[nod] = index;
}
inline int lca ( int x , int y ){
while ( lant[x] != lant[y] ){
if ( nivelnod[tata_lant[lant[x]]] >= nivelnod[tata_lant[lant[y]]] ){
x = tata_lant[lant[x]];
}
else{
y = tata_lant[lant[y]];
}
}
if ( nivelnod[x] <= nivelnod[y] ){
return x;
}
return y;
}
int main () {
fscanf(f,"%d %d",&n,&m);
int x,y;
for ( int i = 2 ; i <= n ; ++i ){
fscanf(f,"%d",&x);
//if ( x == y ) continue ;
G[x].pb(mp(i,mp(i,0)));
G[i].pb(mp(x,mp(i,0)));
}
dfs(1,0);
//DFS(1);
//init_prob(1,1,n);
//dfs_sol(1);
//fprintf(g,"%lf\n",sol);
tata_lant[lant[1]] = 0; nivelnod[0] = -1;
for ( int i = 1 ; i <= m ; ++i ){
fscanf(f,"%d %d",&x,&y);
fprintf(g,"%d\n",lca(x,y));
}
fclose(f);
fclose(g);
return 0;
}