#include <bits/stdc++.h>
using namespace std;
int p[100005][21], h[100005];
vector<int> v[100005];
int tin[100005], tout[100005];
int TIME;
int n;
void dfs(int a, int par) {
  h[a] = h[par] + 1;
  tin[a] = ++TIME;
  for (auto i : v[a]) {
    dfs(i, a);
  }
  tout[a] = ++TIME;
}
void init() {
  for (int i = 1; i <= 18; i++) {
    for (int j = 1; j <= n; j++) {
      p[j][i] = p[p[j][i - 1]][i - 1];
    }
  }
}
int LCA(int u, int v) {
  if (h[v] < h[u])
    for (int i = 17; i >= 0; i--)
      if (h[p[u][i]] >= h[v]) u = p[u][i];
  if (h[u] < h[v])
    for (int i = 17; i >= 0; i--)
      if (h[p[v][i]] >= h[u]) v = p[v][i];
  for (int i = 17; i >= 0; i--)
    if (p[u][i] != p[v][i]) {
      u = p[u][i];
      v = p[v][i];
    }
  while (u != v) {
    u = p[u][0];
    v = p[v][0];
  }
  return u;
}
vector<int> st[100005 * 4];
bool cmp(int a, int b) { return tin[a] < tin[b]; }
vector<int> V;
void build(int id, int l, int r) {
  if (l == r) {
    st[id].push_back(l);
    return;
  }
  int mid = (l + r) / 2;
  build(id * 2, l, mid);
  build(id * 2 + 1, mid + 1, r);
  merge(st[id * 2].begin(), st[id * 2].end(), st[id * 2 + 1].begin(),
        st[id * 2 + 1].end(), back_inserter(st[id]), cmp);
}
int min(int a, int b) {
  if (tin[a] < tin[b]) return a;
  return b;
}
int max(int a, int b) {
  if (tin[a] < tin[b]) return b;
  return a;
}
pair<int, int> get(int id, int l, int r, int u, int v) {
  if (v < l || r < u) return pair<int, int>(n + 1, 0);
  if (u <= l && r <= v) {
    return pair<int, int>(st[id][0], st[id].back());
  }
  int mid = (l + r) / 2;
  pair<int, int> a = get(id * 2, l, mid, u, v);
  pair<int, int> b = get(id * 2 + 1, mid + 1, r, u, v);
  return pair<int, int>(min(a.first, b.first), max(a.second, b.second));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int q;
  cin >> n >> q;
  for (int i = 2; i <= n; i++) {
    cin >> p[i][0];
    v[p[i][0]].push_back(i);
  }
  dfs(1, 0);
  build(1, 1, n);
  tin[n + 1] = 1e9;
  init();
  for (int z = 1; z <= q; z++) {
    int l, r;
    cin >> l >> r;
    pair<int, int> u = get(1, 1, n, l, r);
    int a = u.first;
    int b = u.second;
    pair<int, int> u1 = get(1, 1, n, l, a - 1);
    pair<int, int> u2 = get(1, 1, n, a + 1, r);
    int p1 = min(u1.first, u2.first);
    int p2 = max(u1.second, u2.second);
    int res = LCA(p1, p2);
    res = h[res];
    u1 = get(1, 1, n, l, b - 1);
    u2 = get(1, 1, n, b + 1, r);
    p1 = min(u1.first, u2.first);
    p2 = max(u1.second, u2.second);
    int res1 = LCA(p1, p2);
    res1 = h[res1];
    if (res > res1)
      cout << a << " " << res - 1 << endl;
    else
      cout << b << " " << res1 - 1 << endl;
  }
  return 0;
}
