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 SIZE = 2e5 + 5;
const int inf = 0x7f7f7f7f;
int n, m, num, tim;
int head[SIZE], fa[SIZE], siz[SIZE], son[SIZE], dfn[SIZE], idx[SIZE], bel[SIZE],
    dep[SIZE], ans[SIZE];
struct ms {
  int u, ti, id, key;
  bool operator<(const ms &a) const {
    return dep[u] + ti == dep[a.u] + a.ti ? u < a.u
                                          : dep[u] + ti < dep[a.u] + a.ti;
  }
} a[SIZE];
struct node {
  int to, nxt;
} edge[SIZE << 1];
namespace GTR {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, bufl, stdin);
    if (s == t) return EOF;
  }
  return *s++;
}
inline int read() {
  int a = 0, b = 1, c = fetch();
  while (c < 48 || c > 57) b ^= c == '-', c = fetch();
  while (c >= 48 && c <= 57) a = (a << 1) + (a << 3) + c - 48, c = fetch();
  return b ? a : -a;
}
}  // namespace GTR
using GTR::read;
void addEdge(int u, int v) { edge[++num] = (node){v, head[u]}, head[u] = num; }
void dfs1(int u, int anc) {
  siz[u] = 1;
  for (int i = head[u], v; i; i = edge[i].nxt) {
    v = edge[i].to;
    if (v == anc) continue;
    fa[v] = u, dep[v] = dep[u] + 1;
    dfs1(v, u);
    siz[u] += siz[v];
    if (siz[son[u]] < siz[v]) son[u] = v;
  }
}
void dfs2(int u, int top) {
  bel[u] = top, dfn[u] = ++tim, idx[tim] = u;
  if (son[u]) dfs2(son[u], top);
  for (int i = head[u], v; i; i = edge[i].nxt) {
    v = edge[i].to;
    if (v == fa[u] || v == son[u]) continue;
    dfs2(v, v);
  }
}
namespace segmentTree {
int l[SIZE << 2], r[SIZE << 2], mx[SIZE << 2], tag[SIZE << 2], val[SIZE << 2];
void pushDown(int p) {
  (tag[(p << 1)] = std::max(tag[(p << 1)], tag[p]),
   mx[(p << 1)] = std::max(mx[(p << 1)], val[(p << 1)] + tag[p])),
      (tag[(p << 1 | 1)] = std::max(tag[(p << 1 | 1)], tag[p]),
       mx[(p << 1 | 1)] =
           std::max(mx[(p << 1 | 1)], val[(p << 1 | 1)] + tag[p]));
  tag[p] = -inf;
}
void build(int p, int ql, int qr) {
  l[p] = ql, r[p] = qr, mx[p] = tag[p] = -inf, val[p] = 0;
  if (ql == qr) return val[p] = dep[idx[ql]] * 2, void();
  int mid = (ql + qr) >> 1;
  build((p << 1), ql, mid), build((p << 1 | 1), mid + 1, qr);
  val[p] = std::max(val[(p << 1)], val[(p << 1 | 1)]);
}
int query(int p, int ql, int qr) {
  if (ql <= l[p] && r[p] <= qr) return mx[p];
  if (tag[p] != -inf) pushDown(p);
  int mid = (l[p] + r[p]) >> 1, res = -inf;
  if (ql <= mid) res = std::max(res, query((p << 1), ql, qr));
  if (qr > mid) res = std::max(res, query((p << 1 | 1), ql, qr));
  return res;
}
void modify(int p, int ql, int qr, int x) {
  if (ql <= l[p] && r[p] <= qr)
    return tag[p] = std::max(tag[p], x), mx[p] = std::max(mx[p], val[p] + x),
           void();
  if (tag[p] != -inf) pushDown(p);
  int mid = (l[p] + r[p]) >> 1;
  if (ql <= mid) modify((p << 1), ql, qr, x);
  if (qr > mid) modify((p << 1 | 1), ql, qr, x);
  mx[p] = std::max(mx[(p << 1)], mx[(p << 1 | 1)]);
}
}  // namespace segmentTree
int query(int x, int ti) {
  int u = x;
  while (u) {
    if (segmentTree::query(1, dfn[bel[u]], dfn[u]) <= dep[x] + ti) {
      u = fa[bel[u]];
      continue;
    }
    int l = dfn[bel[u]], r = dfn[u], mid = 0, pos = r;
    while (l <= r) {
      int mid = (l + r) >> 1;
      if (segmentTree::query(1, mid, r) > dep[x] + ti)
        pos = mid, l = mid + 1;
      else
        r = mid - 1;
    }
    u = idx[pos];
    break;
  }
  if (!u) u = 1;
  int res = 2 * (dep[x] - dep[u]) + ti;
  int v = x;
  while (bel[v] != bel[u]) {
    segmentTree::modify(1, dfn[bel[v]], dfn[v], res - dep[x]), v = fa[bel[v]];
  }
  segmentTree::modify(1, dfn[u] + 1, dfn[v], res - dep[x]);
  return res;
}
int main() {
  n = read() + 1, m = read();
  for (int i = 2, anc; i <= n; ++i) {
    anc = read() + 1;
    addEdge(anc, i), addEdge(i, anc);
  }
  for (int i = 1; i <= m; ++i)
    a[i].u = read() + 1, a[i].ti = read(), a[i].id = i;
  dep[1] = 1;
  dfs1(1, 0);
  dfs2(1, 1);
  segmentTree::build(1, 1, n);
  std::sort(a + 1, a + m + 1);
  for (int i = 1; i <= m; ++i) ans[a[i].id] = query(a[i].u, a[i].ti);
  for (int i = 1; i <= m; ++i) printf("%d ", ans[i]);
  puts("");
  return 0;
}