#include <bits/stdc++.h>
using namespace std;
const int logsize = 18;
int n, q, t, dfn[100005], id[100005], dep[100005], pr[100005][logsize];
vector<int> g[100005];
struct segment_min_tree {
  int tr[400005];
  void inline up(int u) { tr[u] = min(tr[u << 1], tr[u << 1 | 1]); }
  void inline build(int u = 1, int l = 1, int r = n) {
    if (l == r) {
      tr[u] = dfn[l];
      return;
    }
    int mid = (l + r) >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    up(u);
  }
  int qry(int el, int er, int u = 1, int l = 1, int r = n) {
    if (l >= el && r <= er) return tr[u];
    int mid = (l + r) >> 1, res = 1e9;
    if (el <= mid) {
      res = min(res, qry(el, er, u << 1, l, mid));
    }
    if (er >= mid + 1) {
      res = min(res, qry(el, er, u << 1 | 1, mid + 1, r));
    }
    return res;
  }
  int inline qry_sec(int el, int er, int mid) {
    int res = 1e9;
    if (el <= mid - 1) {
      res = min(res, qry(el, mid - 1));
    }
    if (er >= mid + 1) {
      res = min(res, qry(mid + 1, er));
    }
    return res;
  }
};
segment_min_tree tmin;
struct segment_max_tree {
  int tr[400005];
  void inline up(int u) { tr[u] = max(tr[u << 1], tr[u << 1 | 1]); }
  void inline build(int u = 1, int l = 1, int r = n) {
    if (l == r) {
      tr[u] = dfn[l];
      return;
    }
    int mid = (l + r) >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    up(u);
  }
  int qry(int el, int er, int u = 1, int l = 1, int r = n) {
    if (l >= el && r <= er) return tr[u];
    int mid = (l + r) >> 1, res = 0;
    if (el <= mid) {
      res = max(res, qry(el, er, u << 1, l, mid));
    }
    if (er >= mid + 1) {
      res = max(res, qry(el, er, u << 1 | 1, mid + 1, r));
    }
    return res;
  }
  int inline qry_sec(int el, int er, int mid) {
    int res = 0;
    if (el <= mid - 1) {
      res = max(res, qry(el, mid - 1));
    }
    if (er >= mid + 1) {
      res = max(res, qry(mid + 1, er));
    }
    return res;
  }
};
segment_max_tree tmax;
int inline read() {
  int res = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); res *= 10, res += c ^ 48, c = getchar())
    ;
  return res;
}
void dfs(int u, int p) {
  dfn[u] = ++t;
  id[t] = u;
  dep[u] = u == 1 ? 0 : dep[p] + 1;
  pr[u][0] = p;
  for (int v : g[u]) {
    dfs(v, u);
  }
}
void inline init() {
  dfs(1, -1);
  for (int i = 1; i <= logsize - 1; ++i) {
    for (int u = 1; u <= n; ++u) {
      int p = pr[u][i - 1];
      if (p == -1) {
        pr[u][i] = -1;
      } else {
        pr[u][i] = pr[p][i - 1];
      }
    }
  }
}
int lca(int u, int v) {
  if (dep[u] > dep[v]) {
    swap(u, v);
  }
  for (int i = 0; i < logsize; ++i) {
    if ((dep[v] - dep[u]) >> i & 1) {
      v = pr[v][i];
    }
  }
  if (u == v) return u;
  for (int i = logsize - 1; i >= 0; --i) {
    if (pr[u][i] != pr[v][i]) {
      u = pr[u][i];
      v = pr[v][i];
    }
  }
  return pr[u][0];
}
int main() {
  ios_base::sync_with_stdio(0);
  n = read();
  q = read();
  for (int u = 2; u <= n; ++u) {
    int p = read();
    g[p].push_back(u);
  }
  init();
  tmin.build();
  tmax.build();
  for (int i = 1; i <= q; ++i) {
    int l = read(), r = read();
    int l1 = tmin.qry(l, r), r1 = tmax.qry(l, r), u1 = id[l1], v1 = id[r1];
    int l2 = tmin.qry_sec(l, r, u1), r2 = tmax.qry_sec(l, r, v1), u2 = id[l2],
        v2 = id[r2];
    int d1 = dep[lca(u1, v2)], d2 = dep[lca(u2, v1)], ban = d1 > d2 ? v1 : u1,
        res = max(d1, d2);
    cout << ban << ' ' << res << '\n';
  }
}
