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;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  int fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= fh;
}
struct Onfo {
  int nu, ne;
} a[200010 * 2];
struct Info {
  int x, t, we, an;
} v[200010];
struct seg {
  int l, r, ma, ta, de;
} t[200010 * 4];
int b[200010], num, m, n, x, so[200010], si[200010], d[200010], f[200010],
    tp[200010], bi[200010], fb[200010];
vector<int> no;
bool co(const Info &a, const Info &b) {
  if (a.t + d[a.x] != b.t + d[b.x]) return a.t + d[a.x] < b.t + d[b.x];
  return a.x < b.x;
}
bool c1(const Info &a, const Info &b) { return a.we < b.we; }
void jb(int x, int y) {
  a[++num].nu = y;
  a[num].ne = b[x];
  b[x] = num;
}
void dfs(int x, int fa, int dep) {
  si[x] = 1;
  so[x] = 0;
  f[x] = fa;
  d[x] = dep;
  for (int y = b[x]; y; y = a[y].ne) {
    if (a[y].nu != fa) {
      dfs(a[y].nu, x, dep + 1);
      si[x] += si[a[y].nu];
      if (si[a[y].nu] > si[so[x]]) so[x] = a[y].nu;
    }
  }
}
void dfs1(int x) {
  bi[x] = ++num;
  fb[num] = x;
  if (so[x] != 0) {
    tp[so[x]] = tp[x];
    dfs1(so[x]);
  }
  for (int y = b[x]; y; y = a[y].ne) {
    if (a[y].nu != f[x] && a[y].nu != so[x]) {
      tp[a[y].nu] = a[y].nu;
      dfs1(a[y].nu);
    }
  }
}
void build(int nu, int l, int r) {
  t[nu].ma = -2e9;
  t[nu].l = l;
  t[nu].r = r;
  t[nu].ta = -2e9;
  if (l != r) {
    build(nu * 2, l, (l + r) / 2);
    build(nu * 2 + 1, (l + r) / 2 + 1, r);
    t[nu].de = t[nu * 2 + 1].de;
  } else {
    t[nu].de = d[fb[l]];
  }
}
void ya(int nu) {
  if (t[nu].ta != -2e9) {
    if (t[nu].l != t[nu].r) {
      t[nu * 2].ta = t[nu].ta;
      t[nu * 2 + 1].ta = t[nu].ta;
    }
    t[nu].ma = t[nu].ta + t[nu].de * 2;
    t[nu].ta = -2e9;
  }
}
int que(int nu, int l, int r) {
  ya(nu);
  if (t[nu].l == l && t[nu].r == r) return t[nu].ma;
  int mid = (t[nu].l + t[nu].r) / 2;
  if (l > mid) return que(nu * 2 + 1, l, r);
  if (r <= mid) return que(nu * 2, l, r);
  return max(que(nu * 2, l, mid), que(nu * 2 + 1, mid + 1, r));
}
void get(int nu, int l, int r) {
  ya(nu);
  if (t[nu].l == l && t[nu].r == r) {
    no.push_back(nu);
    return;
  }
  int mid = (t[nu].l + t[nu].r) / 2;
  if (l <= mid) get(nu * 2, l, min(mid, r));
  if (r > mid) get(nu * 2 + 1, max(mid + 1, l), r);
}
int getans(int nu, int y) {
  if (t[nu].l == t[nu].r) return fb[t[nu].l];
  ya(nu * 2 + 1);
  if (t[nu * 2 + 1].ma <= y) return getans(nu * 2, y);
  return getans(nu * 2 + 1, y);
}
int get(int x, int y) {
  while (x != 0) {
    if (que(1, bi[tp[x]], bi[x]) > y) {
      no.clear();
      get(1, bi[tp[x]], bi[x]);
      for (int i = no.size() - 1; i >= 0; i--) {
        if (t[no[i]].ma > y) return getans(no[i], y);
      }
    }
    x = f[tp[x]];
  }
  return x;
}
void add(int nu, int l, int r, int x) {
  ya(nu);
  if (t[nu].l == l && t[nu].r == r) {
    t[nu].ta = x;
    return;
  }
  int mid = (t[nu].l + t[nu].r) / 2;
  if (l <= mid) add(nu * 2, l, min(mid, r), x);
  if (r > mid) add(nu * 2 + 1, max(mid + 1, l), r, x);
  ya(nu * 2);
  ya(nu * 2 + 1);
  t[nu].ma = max(t[nu * 2].ma, t[nu * 2 + 1].ma);
}
void fu(int x, int y, int z) {
  while (tp[x] != tp[y]) {
    add(1, bi[tp[x]], bi[x], z);
    x = f[tp[x]];
  }
  if (x != y) add(1, bi[y] + 1, bi[x], z);
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) {
    read(x);
    jb(x + 1, i + 1);
    jb(i + 1, x + 1);
  }
  n++;
  dfs(1, 0, 1);
  num = 0;
  tp[1] = 1;
  dfs1(1);
  build(1, 1, n);
  add(1, 1, 1, 2e9);
  for (int i = 1; i <= m; i++) {
    read(v[i].x);
    read(v[i].t);
    v[i].x++;
    v[i].we = i;
  }
  sort(v + 1, v + m + 1, co);
  for (int i = 1; i <= m; i++) {
    v[i].an = get(v[i].x, v[i].t + d[v[i].x]);
    fu(v[i].x, v[i].an, v[i].t + d[v[i].x] - d[v[i].an] * 2);
  }
  sort(v + 1, v + m + 1, c1);
  for (int i = 1; i <= m; i++)
    printf("%d ", v[i].t + d[v[i].x] * 2 - d[v[i].an] * 2);
  puts("");
  return 0;
}