#include<fstream>
#include<vector>
using namespace std;
ifstream cin("lca.in");
ofstream cout("lca.out");
int n,m,a[22][100100],lg[100100], x, y,o=1,poz[2000100];
vector<int> g[100100];
void dfs(int x){
a[0][o]=x;
o++;
for(vector<int>::iterator it = g[x].begin(); it != g[x].end(); it++){
dfs(*it);
a[0][o]=x;
o++;
}
}
int main(){
cin >> n >> m;
for(int i = 1; i < n; i++){
cin >> x;
g[x].push_back(i+1);
}
dfs(1);
for(int i = 1; i < o; i++){
if(!poz[a[0][i]])
poz[a[0][i]] = i;
if(i >= 2)
lg[i] = lg[i/2]+1;
}
for(int i = 1;(1<<i) < o; i++){
int p = 1<<(i-1),k = o-(1<<i)+1;
for(int j = 1; j <= k; j++)
a[i][j] = min(a[i-1][j],a[i-1][p+j]);
}
while(m--){
cin >> x >> y;
x = poz[x];
y = poz[y];
int aux = lg[y-x+1];
cout << min(a[aux][x],a[aux][y-(1<<aux)+1])<<"\n";
}
}