Problem: Alice and Bob are well-known for sending messages to each other. This time you have a rooted tree with Bob standing in the root node and copies of Alice standing in each of the other vertices. The root node has number 0, the rest are numbered 1 through n.

At some moments of time some copies of Alice want to send a message to Bob and receive an answer. We will call this copy the initiator. The process of sending a message contains several steps: 

  * The initiator sends the message to the person standing in the parent node and begins waiting for the answer. 
  * When some copy of Alice receives a message from some of her children nodes, she sends the message to the person standing in the parent node and begins waiting for the answer. 
  * When Bob receives a message from some of his child nodes, he immediately sends the answer to the child node where the message came from. 
  * When some copy of Alice (except for initiator) receives an answer she is waiting for, she immediately sends it to the child vertex where the message came from. 
  * When the initiator receives the answer she is waiting for, she doesn't send it to anybody. 
  * There is a special case: a copy of Alice can't wait for two answers at the same time, so if some copy of Alice receives a message from her child node while she already waits for some answer, she rejects the message and sends a message saying this back to the child node where the message came from. Then the copy of Alice in the child vertex processes this answer as if it was from Bob. 
  * The process of sending a message to a parent node or to a child node is instant but a receiver (a parent or a child) gets a message after 1 second. 



If some copy of Alice receives several messages from child nodes at the same moment while she isn't waiting for an answer, she processes the message from the initiator with the smallest number and rejects all the rest. If some copy of Alice receives messages from children nodes and also receives the answer she is waiting for at the same instant, then Alice first processes the answer, then immediately continue as normal with the incoming messages.

You are given the moments of time when some copy of Alice becomes the initiator and sends a message to Bob. For each message, find the moment of time when the answer (either from Bob or some copy of Alice) will be received by the initiator.

You can assume that if Alice wants to send a message (i.e. become the initiator) while waiting for some answer, she immediately rejects the message and receives an answer from herself in no time.

Input

The first line of input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of nodes with Alices and the number of messages.

Second line contains n integers p1, p2, ..., pn (0 ≤ pi < i). The integer pi is the number of the parent node of node i.

The next m lines describe the messages. The i-th of them contains two integers xi and ti (1 ≤ xi ≤ n, 1 ≤ ti ≤ 109) — the number of the vertex of the initiator of the i-th message and the time of the initiation (in seconds). The messages are given in order of increasing initiation time (i.e. ti + 1 ≥ ti holds for 1 ≤ i < m). The pairs (xi, ti) are distinct.

Output

Print m integers — the i-th of them is the moment of time when the answer for the i-th message will be received by the initiator.

Examples

Input

6 3
0 1 2 3 2 5
4 6
6 9
5 11


Output

14 13 11 

Input

3 2
0 1 1
2 1
3 1


Output

5 3 

Input

8 3
0 1 1 2 3 3 4 5
6 1
8 2
4 5


Output

7 6 11 

Note

In the first example the first message is initiated at the moment 6, reaches Bob at the moment 10, and the answer reaches the initiator at the moment 14. The second message reaches vertex 2 at the moment 11. At this moment the copy of Alice in this vertex is still waiting for the answer for the first message, so she rejects the second message. The answer reaches the initiator at the moment 13. The third message is not sent at all, because at the moment 11 Alice in vertex 5 is waiting for the answer for the second message.

In the second example the first message reaches Bob, the second is rejected by Alice in vertex 1. This is because the message with smaller initiator number has the priority.

In the third example the first and the third messages reach Bob, while the second message is rejected by Alice in vertex 3.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int get() {
  char ch;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-') {
    int s = 0;
    while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
    return -s;
  }
  int s = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
  return s;
}
const int N = 2e5 + 5;
const int INF = 1e9;
int n, m;
int fa[N];
int lef[N], rig[N];
int dep[N];
struct initiator {
  int x, tim, val, id;
  initiator(const int x_ = 0, const int tim_ = 0, const int val_ = 0,
            const int id_ = 0) {
    x = x_;
    tim = tim_;
    val = val_;
    id = id_;
  }
} key[N];
int siz[N], mx[N];
int dfn[N], k, R[N];
int d[N];
int be[N], u, tp[N];
void dfs1(int x) {
  siz[x] = 1;
  for (int y = lef[x]; y; y = rig[y]) {
    dep[y] = dep[x] + 1;
    dfs1(y);
    siz[x] += siz[y];
    if (!mx[x] || siz[y] > siz[mx[x]]) mx[x] = y;
  }
}
void dfs2(int x) {
  d[dfn[x] = ++k] = x;
  if (!mx[x]) {
    R[x] = k;
    tp[be[x] = ++u] = x;
    return;
  }
  dfs2(mx[x]);
  tp[be[x] = be[mx[x]]] = x;
  for (int y = lef[x]; y; y = rig[y])
    if (y != mx[x]) dfs2(y);
  R[x] = k;
}
int ans[N];
bool cmp(initiator a, initiator b) {
  if (a.val != b.val) return a.val < b.val;
  return a.id < b.id;
}
struct node {
  int l, r, val;
  int ad;
} tree[N * 2];
int rt, tot;
void build(int &now, int l, int r) {
  tree[now = ++tot].val = -INF;
  tree[now].ad = -INF;
  if (l == r) {
    if (l == 1) tree[now].val = INF * 2;
    return;
  }
  int mid = (l + r) / 2;
  build(tree[now].l, l, mid);
  build(tree[now].r, mid + 1, r);
  tree[now].val = max(tree[tree[now].l].val, tree[tree[now].r].val);
}
void down(int now, int l, int r) {
  int mid = (l + r) / 2;
  if (tree[now].ad != -INF) {
    tree[tree[now].l].ad = tree[tree[now].r].ad = tree[now].ad;
    tree[tree[now].l].val = tree[now].ad + dep[d[mid]] * 2;
    tree[tree[now].r].val = tree[now].ad + dep[d[r]] * 2;
    tree[now].ad = -INF;
  }
}
void change(int now, int l, int r, int x, int y, int v) {
  if (x <= l && r <= y) {
    tree[now].ad = v;
    tree[now].val = v + dep[d[r]] * 2;
    return;
  }
  down(now, l, r);
  int mid = (l + r) / 2;
  if (x <= mid) change(tree[now].l, l, mid, x, y, v);
  if (y > mid) change(tree[now].r, mid + 1, r, x, y, v);
  tree[now].val = max(tree[tree[now].l].val, tree[tree[now].r].val);
}
int getw(int now, int l, int r, int x, int y, int v) {
  if (tree[now].val <= v) return -1;
  if (x <= l && r <= y) {
    if (l == r) return l;
    int mid = (l + r) / 2;
    down(now, l, r);
    if (tree[tree[now].r].val > v)
      return getw(tree[now].r, mid + 1, r, x, y, v);
    return getw(tree[now].l, l, mid, x, y, v);
  }
  int mid = (l + r) / 2, ret = -1;
  down(now, l, r);
  if (y > mid) ret = getw(tree[now].r, mid + 1, r, x, y, v);
  if (ret == -1 & x <= mid) ret = getw(tree[now].l, l, mid, x, y, v);
  return ret;
}
int add(int x, int t) {
  t += dep[x];
  int now = x, px = x;
  while (now) {
    int w = getw(1, 1, n, dfn[tp[be[now]]], dfn[now], t);
    if (w != -1) {
      now = d[w];
      break;
    }
    now = fa[tp[be[now]]];
  }
  int ret = t - dep[now] + dep[x] - dep[now];
  int val = t - dep[now] * 2;
  int to = now;
  now = x;
  while (now != to) {
    if (be[now] != be[to]) {
      change(1, 1, n, dfn[tp[be[now]]], dfn[now], val);
      now = fa[tp[be[now]]];
    } else {
      change(1, 1, n, dfn[to] + 1, dfn[now], val);
      now = to;
    }
  }
  return ret;
}
int kth[N], tim[N], que[N], ul;
struct edge {
  int x, nxt;
} e[N];
int h[N], et;
int tmp[N], tl;
void inse(int x, int y) {
  e[++et].x = y;
  e[et].nxt = h[x];
  h[x] = et;
}
bool cmp2(int x, int y) { return kth[x] < kth[y]; }
int rearrange(int st, int l, int r) {
  for (; l <= r && dfn[que[l]] <= R[st];) {
    inse(st, que[l]);
    l = rearrange(que[l], l + 1, r);
  }
  tl = 0;
  for (int p = h[st]; p; p = e[p].nxt) tmp[++tl] = e[p].x;
  sort(tmp + 1, tmp + 1 + tl);
  for (int p = h[st], t = 1; p; p = e[p].nxt, t++) e[p].x = tmp[t];
  return l;
}
void rg(int x) {
  if (x <= n) que[++ul] = x;
  for (int p = h[x]; p; p = e[p].nxt) rg(e[p].x);
}
bool cmp1(int x, int y) { return dfn[x] < dfn[y]; }
int main() {
  n = get() + 1;
  m = get();
  for (int i = 2; i <= n; i++) {
    fa[i] = get() + 1;
    rig[i] = lef[fa[i]];
    lef[fa[i]] = i;
  }
  dep[1] = 1;
  dfs1(1);
  dfs2(1);
  build(rt, 1, n);
  for (int i = 1; i <= m; i++) {
    int x = get() + 1, t = get();
    key[i] = initiator(x, t, dep[x] + t, i);
  }
  sort(key + 1, key + 1 + m, cmp);
  int last = 0;
  for (int i = 1; i <= m; i++)
    if (i == m || key[i].val != key[i + 1].val) {
      ul = et = 0;
      for (int j = last + 1; j <= i; j++) {
        int x = key[j].x, t = key[j].tim;
        kth[x] = key[j].id, tim[x] = t;
        que[++ul] = x;
        h[x] = 0;
      }
      sort(que + 1, que + 1 + ul, cmp1);
      h[n + 1] = 0;
      R[n + 1] = n + 1;
      rearrange(n + 1, 1, ul);
      ul = 0;
      rg(n + 1);
      for (int j = 1; j <= ul; j++) {
        int x = que[j];
        ans[kth[x]] = add(x, tim[x]);
      }
      last = i;
    }
  for (int i = 1; i <= m; i++) printf("%d%c", ans[i], i == m ? '\n' : ' ');
  return 0;
}