#include <bits/stdc++.h>

#define FILENAME "MAIN"
#define ll long long 
#define el cout << '\n'
#define ii pair<ll, ll>
#define fi first 
#define se second 
#define pb push_back
#define YES cout << "YES", el
#define NO cout << "NO", el
#define print_type cout
#define print_el print_type << '\n'
#define DEBUG(...) [](auto && ... x) {int i = 0; ((print_type << (i++ ? " " : "") << x), ...), print_el;} (__VA_ARGS__)
#define bit(mask, i) (((mask) >> (i)) & 1)
#define BIT(n) (1ll << (n))

using namespace std;

const bool is_brute = 0;
const bool multi_test = 0;

const int maxn = 2e5;
const int maxlog = 17;

int n, q, up[maxn + 10][maxlog + 10], tin[maxn + 10], tout[maxn + 10], timer = 0;
vector<int> adj[maxn + 10];

void DFS(int top)
{
    tin[top] = ++timer;
    for (int next_top : adj[top])
    {
        if (next_top == up[top][0])
            continue;
        up[next_top][0] = top;
        DFS(next_top);
    }
    tout[top] = timer;
}
bool isAncestor(int x, int y)
{
    if (x == 0)
        return 1;
    return tin[x] <= tin[y] && tin[y] <= tout[x];
}
int getLCA(int x, int y)
{
    if (isAncestor(x, y))
        return x;
    if (isAncestor(y, x))
        return y;
    for (int i = maxlog; i >= 0; i--)
        if (!isAncestor(up[x][i], y))
            x = up[x][i];
    return up[x][0];
}

void solve()
{
    cin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        int p;
        cin >> p;
        adj[p].push_back(i);
    }
    DFS(1);
    for (int j = 1; j <= maxlog; j++)
        for (int i = 1; i <= n; i++)
            up[i][j] = up[up[i][j - 1]][j - 1];
    while (q--)
    {
        int u, v;
        cin >> u >> v;
        cout << getLCA(u, v), el;
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(FILENAME".INP", "r"))
    {
        freopen(FILENAME".INP", "r", stdin);
        if (is_brute)
            freopen(FILENAME"_TRAU.OUT", "w", stdout);
        else
            freopen(FILENAME".OUT", "w", stdout);
    }

    int ntest;
    if (multi_test)
        cin >> ntest;
    else
        ntest = 1;
    for (int itest = 1; itest <= ntest; itest++)
    {
        // cout << itest, el;
        solve();
    }
}