#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct bian {
  int from, to;
} eg[N << 1];
int front[N], num;
void add(int x, int y) {
  eg[++num].to = y;
  eg[num].from = front[x];
  front[x] = num;
}
int n, q;
int fa[N], siz[N], dep[N], son[N], top[N], dfn[N];
void dfs1(int o, int from) {
  fa[o] = from;
  siz[o] = 1;
  dep[o] = dep[from] + 1;
  for (int i = front[o]; i; i = eg[i].from) {
    int to = eg[i].to;
    if (to == from) continue;
    dfs1(to, o);
    siz[o] += siz[to];
    if (siz[to] > siz[son[o]]) son[o] = to;
  }
}
void dfs2(int o, int tp) {
  top[o] = tp;
  dfn[o] = ++num;
  if (son[o]) dfs2(son[o], tp);
  for (int i = front[o]; i; i = eg[i].from) {
    int to = eg[i].to;
    if (to == fa[o] || to == son[o]) continue;
    dfs2(to, to);
  }
}
int LCA(int x, int y) {
  if (x == 0 || y == 0) return x + y;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    x = fa[top[x]];
  }
  if (dep[x] < dep[y]) swap(x, y);
  return y;
}
pair<int, int> mins[N << 2], maxs[N << 2];
int lca[N << 2];
void build(int o, int l, int r) {
  if (l == r) {
    mins[o] = make_pair(dfn[l], l);
    maxs[o] = make_pair(dfn[l], l);
    lca[o] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(o << 1, l, mid);
  build(o << 1 | 1, mid + 1, r);
  mins[o] = min(mins[o << 1], mins[o << 1 | 1]);
  maxs[o] = max(maxs[o << 1], maxs[o << 1 | 1]);
  lca[o] = LCA(lca[o << 1], lca[o << 1 | 1]);
}
pair<int, int> que1(int o, int l, int r, int L, int R) {
  if (L <= l && r <= R) return mins[o];
  int mid = (l + r) >> 1;
  pair<int, int> re = make_pair(1e9, 0);
  if (mid >= L) re = que1(o << 1, l, mid, L, R);
  if (mid < R) re = min(re, que1(o << 1 | 1, mid + 1, r, L, R));
  return re;
}
pair<int, int> que2(int o, int l, int r, int L, int R) {
  if (L <= l && r <= R) return maxs[o];
  int mid = (l + r) >> 1;
  pair<int, int> re = make_pair(-1e9, 0);
  if (mid >= L) re = que2(o << 1, l, mid, L, R);
  if (mid < R) re = max(re, que2(o << 1 | 1, mid + 1, r, L, R));
  return re;
}
int que3(int o, int l, int r, int L, int R) {
  if (L > R) return 0;
  if (L <= l && r <= R) return lca[o];
  int mid = (l + r) >> 1, re = 0;
  if (mid >= L) re = que3(o << 1, l, mid, L, R);
  if (mid < R)
    re = re == 0 ? que3(o << 1 | 1, mid + 1, r, L, R)
                 : LCA(que3(o << 1 | 1, mid + 1, r, L, R), re);
  return re;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 2; i <= n; ++i) {
    int fa;
    scanf("%d", &fa);
    add(fa, i);
    add(i, fa);
  }
  num = 0;
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    int mins = que1(1, 1, n, l, r).second, maxs = que2(1, 1, n, l, r).second;
    int ans1 =
            dep[LCA(que3(1, 1, n, l, mins - 1), que3(1, 1, n, mins + 1, r))] -
            1,
        ans2 =
            dep[LCA(que3(1, 1, n, l, maxs - 1), que3(1, 1, n, maxs + 1, r))] -
            1;
    printf("%d %d\n", ans1 > ans2 ? mins : maxs, max(ans1, ans2));
  }
}
