#include <fstream>
#include <vector>
#define NMAX 100000
using namespace std;
ifstream fin("lca.in");
ofstream fout("lca.out");
/// euler - contine nodurile in reprezentarea euleriana
int n, m, parinte[NMAX + 5], first[NMAX + 5], height[NMAX + 5], log[2 * NMAX + 5];
vector<int> fii[NMAX + 5], euler;
int RMQ[18][2 * NMAX + 5];
void citireArbore();
void buildEuler(int nod, int h);
void buildRMQ();
int LCA(int x, int y);
int main(){
citireArbore();
buildEuler(1, 0);
buildRMQ();
int x, y;
while(m--){
fin >> x >> y;
fout << LCA(x, y) << '\n';
}
fin.close();
fout.close();
return 0;
}
int LCA(int x, int y){
x = first[x];
y = first[y];
if(x > y) swap(x, y);
int p2 = log[y - x + 1];
int len = 1 << p2;
// vrem NODUL, NU inaltimea
return (height[RMQ[p2][x]] < height[RMQ[p2][y - len + 1]] ?
RMQ[p2][x] : RMQ[p2][y - len + 1]);
}
void buildRMQ(){
//! IMPORTANT: euler & RMQ sunt 0-indexed !
//! RMQ contine NODURILE de inaltime minima pe intervalele coresp.
int Ne = euler.size();
for(int i = 0; i < Ne; i++)
RMQ[0][i] = euler[i];
for(int p = 1; (1 << p) <= Ne; p++){
for(int i = 0; i < Ne; i++){
RMQ[p][i] = RMQ[p - 1][i];
int j = i + (1 << (p - 1));
// vrem NODUL, NU inaltimea
if(j < Ne)
RMQ[p][i] = (height[RMQ[p][i]] < height[RMQ[p - 1][j]] ?
RMQ[p][i] : RMQ[p - 1][j]);
}
}
log[1] = 0;
for(int i = 2; i <= Ne; i++)
log[i] = log[i >> 1] + 1;
}
void buildEuler(int nod, int h){
height[nod] = h;
first[nod] = euler.size();
euler.push_back(nod);
for(auto i : fii[nod]){
buildEuler(i, h + 1);
euler.push_back(nod);
}
}
void citireArbore(){
int x;
/// nodul 1 este radacina
fin >> n >> m;
for(int i = 2; i <= n; i++){
fin >> x;
fii[x].push_back(i);
}
}