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;
const int N = 200200, D = 2;
int i, j, k, n, m, nm, Tn, ch, En, x, y, fx, s;
int h[N], p[N], ans[N];
struct tree {
  int s, ma, pos;
} T[N << 2];
struct cc {
  int x, t, q;
} A[N];
struct dd {
  int l, r;
} B[N];
struct point {
  int fa, dp, sz, sn, fv, tp;
} P[N];
struct edge {
  int s, n;
} E[N];
void R(int &x) {
  x = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) ch = getchar();
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
void W(int x) {
  if (x >= 10) W(x / 10);
  putchar(x % 10 + '0');
}
void E_add(int x, int y) {
  E[++En].s = y;
  E[En].n = h[x];
  h[x] = En;
}
void dfs1(int x) {
  P[x].sz = 1;
  for (int k = h[x]; k; k = E[k].n) {
    P[E[k].s].dp = P[x].dp + 1;
    dfs1(E[k].s);
    P[x].sz += P[E[k].s].sz;
    if (P[E[k].s].sz > P[P[x].sn].sz) P[x].sn = E[k].s;
  }
}
void dfs2(int x, int top) {
  P[x].fv = ++Tn;
  p[Tn] = x;
  P[x].tp = top;
  if (P[x].sn) dfs2(P[x].sn, top);
  for (int k = h[x]; k; k = E[k].n)
    if (E[k].s != P[x].sn) dfs2(E[k].s, E[k].s);
}
bool cmp(const cc &a, const cc &b) {
  if (P[a.x].dp + a.t == P[b.x].dp + b.t) return a.x < b.x;
  return P[a.x].dp + a.t < P[b.x].dp + b.t;
}
int max(const int &x, const int &y) {
  if (x < y) return y;
  return y;
}
void up(int k) {
  if (T[k << 1].ma > T[k << 1 | 1].ma)
    T[k].ma = T[k << 1].ma, T[k].pos = T[k << 1].pos;
  else
    T[k].ma = T[k << 1 | 1].ma, T[k].pos = T[k << 1 | 1].pos;
}
void down(int k, int L, int R) {
  if (T[k].s) {
    int mid = (L + R) >> 1;
    T[k << 1].s = T[k].s;
    T[k << 1].ma = T[k].s + (mid - L) * D;
    T[k << 1].pos = mid;
    T[k << 1 | 1].s = T[k << 1].ma + D;
    T[k << 1 | 1].ma = T[k].s + (R - L) * D;
    T[k << 1 | 1].pos = R;
    T[k].s = 0;
  }
}
int T_find(int L, int R, int l, int r, int x, int k) {
  if (L == R) {
    if (T[k].ma > x) return R;
    return -1;
  }
  down(k, L, R);
  int mid = (L + R) >> 1;
  if (L == l && R == r) {
    if (T[k << 1 | 1].ma > x)
      return T_find(mid + 1, R, mid + 1, r, x, k << 1 | 1);
    if (T[k << 1].ma > x) return T_find(L, mid, l, mid, x, k << 1);
    return -1;
  }
  if (r <= mid) return T_find(L, mid, l, r, x, k << 1);
  if (l > mid) return T_find(mid + 1, R, l, r, x, k << 1 | 1);
  int t = T_find(mid + 1, R, mid + 1, r, x, k << 1 | 1);
  if (t != -1) return t;
  return T_find(L, mid, l, mid, x, k << 1);
}
void T_add(int L, int R, int l, int r, int s, int k) {
  if (L == l && R == r) {
    T[k].s = s;
    T[k].ma = s + (R - L) * D;
    T[k].pos = R;
    return;
  }
  down(k, L, R);
  int mid = (L + R) >> 1;
  if (r <= mid)
    T_add(L, mid, l, r, s, k << 1);
  else {
    if (l > mid)
      T_add(mid + 1, R, l, r, s, k << 1 | 1);
    else
      T_add(L, mid, l, mid, s, k << 1),
          T_add(mid + 1, R, mid + 1, r, s + (mid - l + 1) * D, k << 1 | 1);
  }
  up(k);
}
int main() {
  R(n);
  n++;
  R(m);
  for (i = 2; i <= n; i++) {
    R(P[i].fa);
    P[i].fa++;
    E_add(P[i].fa, i);
  }
  dfs1(1);
  dfs2(1, 1);
  for (i = 1; i <= m; i++) {
    R(A[i].x);
    R(A[i].t);
    A[i].q = i;
    A[i].x++;
  }
  sort(A + 1, A + m + 1, cmp);
  for (i = 1; i <= m; i++) {
    x = A[i].x;
    fx = P[x].tp;
    nm = 0;
    while (x) {
      y = T_find(1, Tn, P[fx].fv, P[x].fv, A[i].t + P[A[i].x].dp, 1);
      if (y == -1) {
        B[++nm].l = P[fx].fv;
        B[nm].r = P[x].fv;
      } else {
        B[++nm].l = y;
        B[nm].r = P[x].fv;
        break;
      }
      x = P[fx].fa;
      fx = P[x].tp;
    }
    ans[A[i].q] = A[i].t + 2 * (P[A[i].x].dp - P[p[y]].dp);
    if (y == -1) {
      s = A[i].t + P[A[i].x].dp;
      while (nm) {
        T_add(1, Tn, B[nm].l, B[nm].r, s, 1);
        s += (B[nm].r - B[nm].l + 1) * D;
        nm--;
      }
    } else {
      s = A[i].t + P[A[i].x].dp + D;
      if (B[nm].l == B[nm].r)
        nm--;
      else
        B[nm].l++;
      while (nm) {
        T_add(1, Tn, B[nm].l, B[nm].r, s, 1);
        s += (B[nm].r - B[nm].l + 1) * D;
        nm--;
      }
    }
  }
  for (i = 1; i <= m; i++) W(ans[i]), putchar(' ');
  puts("");
}