#include<fstream>
#include<vector>
#define EMAX 200010
#define ARBMAX 600010
#define NMAX 100010
using namespace std;
ifstream f("lca.in");
ofstream g("lca.out");
vector<int> a[NMAX];
int n, m, e[EMAX], arb[ARBMAX], p_ap[NMAX], nivel[NMAX];
void Citeste()
{
int i, x;
f>>n>>m;
for (i=2; i<=n; ++i)
{
f>>x;
a[x].push_back(i);
a[i].push_back(x);
}
}
void Update(int st, int dr, int pz, int val, int nod)
{
if(st==dr) arb[nod]=val;
else
{
int mij=(st+dr)/2;
if (pz<=mij) Update(st, mij, pz, val, nod*2);
else Update(mij+1, dr, pz, val, nod*2+1);
if (nivel[arb[nod*2]]<nivel[arb[nod*2+1]]) arb[nod]=arb[nod*2];
else arb[nod]=arb[nod*2+1];
}
}
int Euler(int nod, int niv)
{
int i, r;
nivel[nod]=niv; e[++e[0]]=nod; p_ap[nod]=e[0];
for (i=0; i<a[nod].size(); ++i)
{
r=a[nod][i];
if (!nivel[r])
{
Euler(r, niv+1);
e[++e[0]]=nod;
}
}
}
int query(int st, int dr, int x, int y, int nod)
{
if (y<st || x>dr) return n+1;
if (x<=st && y>=dr) return arb[nod];
int mij=(st+dr)/2, rez1, rez2;
rez1=query(st, mij, x, y, nod*2);
rez2=query(mij+1, dr, x, y, nod*2+1);
if (nivel[rez1]<nivel[rez2]) return rez1;
return rez2;
}
void Solve()
{
int i, x, y;
nivel[n+1]=e[0]+1;
for (i=1; i<=e[0]*3; ++i) arb[i]=n+1;
for (i=1; i<=e[0]; ++i)
Update(1, e[0], i, e[i], 1);
while (m--)
{
f>>x>>y;
x=p_ap[x]; y=p_ap[y];
if (x>y) swap(x, y);
g<<query(1, e[0], x, y, 1)<<"\n";
}
}
int main()
{
Citeste();
Euler(1, 1);
Solve();
f.close();
g.close();
return 0;
}