// https://infoarena.ro/problema/lca
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize("O3,unroll-loops")
#pragma GCC target( \
"avx512f,avx512bw,avx512dq,avx512cd,avx512vl,bmi,bmi2,lzcnt,popcnt,fma")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,fma")
// #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
using uint64 = uint64_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int16 = int16_t;
using uint16 = uint16_t;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rfor(i, st, dr) for (auto i = (st); i <= (dr); ++i)
#define for0(i, n) for (auto i = 0; i < (n); ++i)
#define rfor0(i, n) for (auto i = (n) - 1; i >= 0; --i)
#define for1(i, n) for (auto i = 1; i <= (n); ++i)
#define rfor1(i, n) for (auto i = (n); i >= 1; --i)
#define foreach(x, a) for (auto &x : a)
#define cforeach(x, a) for (const auto &x : a)
#define ft first
#define sd second
#define cendl cout << "\n"
#define fendl fout << "\n"
#define FASTIO \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
// #define int int64
ifstream fin("lca.in");
ofstream fout("lca.out");
// FILE* fin = fopen("", "r");
// FILE* fout = fopen("", "w");
const int NRMAX = 100000;
const int LGMAX = 16;
int arb[LGMAX + 1][NRMAX + 1], lvl[NRMAX + 1], n;
int log2(int x) {
int rez = 0;
while (x > 1) {
x >>= 1;
++rez;
}
return rez;
}
int lca(int a, int b) {
if (lvl[a] > lvl[b])
swap(a, b);
if (lvl[a] != lvl[b]) {
for (int j = log2(lvl[b] - lvl[a]); lvl[b] > lvl[a] && j >= 0; --j) {
if (lvl[b] - (1 << j) >= lvl[a])
b = arb[j][b];
}
}
if (a != b) {
for (int j = log2(lvl[a]); j >= 0; --j) {
if (arb[j][a] && arb[j][a] != arb[j][b]) {
a = arb[j][a];
b = arb[j][b];
}
}
a = b = arb[0][a];
}
return a;
}
void build() {
for (int i = 2; i <= n; ++i) {
lvl[i] = lvl[arb[0][i]] + 1;
}
for (int k = 1; (1 << k) <= n; ++k) {
for (int i = 2; i <= n; ++i) {
arb[k][i] = arb[k - 1][arb[k - 1][i]];
}
}
}
int32 main() {
// FASTIO;
int m, i, a, b;
fin >> n >> m;
for (i = 2; i <= n; ++i) {
fin >> arb[0][i];
}
build();
while (m--) {
fin >> a >> b;
fout << lca(a, b) << "\n";
}
return 0;
}