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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
using namespace std;
namespace output {
void __(short x) { cout << x; }
void __(unsigned x) { cout << x; }
void __(int x) { cout << x; }
void __(long long x) { cout << x; }
void __(unsigned long long x) { cout << x; }
void __(double x) { cout << x; }
void __(long double x) { cout << x; }
void __(char x) { cout << x; }
void __(const char* x) { cout << x; }
void __(const string& x) { cout << x; }
void __(bool x) { cout << (x ? "true" : "false"); }
template <class S, class T>
void __(const pair<S, T>& x) {
  __(1 ? "(" : ""), __(x.first), __(1 ? ", " : " "), __(x.second),
      __(1 ? ")" : "");
}
template <class T>
void __(const vector<T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
template <class T>
void __(const set<T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
template <class T>
void __(const multiset<T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
template <class S, class T>
void __(const map<S, T>& x) {
  __(1 ? "{" : "");
  bool _ = 0;
  for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1;
  __(1 ? "}" : "");
}
void pr() { cout << "\n"; }
template <class S, class... T>
void pr(const S& a, const T&... b) {
  __(a);
  if (sizeof...(b)) __(' ');
  pr(b...);
}
}  // namespace output
using namespace output;
const int MN = 2e5 + 5;
int N, M, i, j, x, y, t, sz[MN], lnk[MN], dep[MN], par[MN], idx[MN], rev[MN],
    nxt, ans[MN];
vector<int> adj[MN];
struct qur {
  int x, y, id;
} q[MN];
struct nd {
  int mx;
  pair<int, int> lz;
} st[3 * MN];
void dfs(int n) {
  sz[n] = 1;
  pair<int, int> big(-1, -1);
  for (auto v : adj[n]) {
    dep[v] = dep[n] + 1;
    par[v] = n;
    dfs(v);
    sz[n] += sz[v];
    if (sz[v] > big.first) big = {sz[v], v};
  }
  if (big.second != -1) lnk[big.second] = n;
}
void dfs2(int n) {
  if (lnk[n] == -1)
    lnk[n] = n, idx[n] = ++nxt, rev[nxt] = n;
  else
    lnk[n] = lnk[lnk[n]], idx[n] = ++nxt, rev[nxt] = n;
  int hmm = -1;
  for (auto v : adj[n]) {
    if (lnk[v] == n) {
      dfs2(v);
      hmm = v;
      break;
    }
  }
  for (auto v : adj[n]) {
    if (v != hmm) dfs2(v);
  }
}
inline void upd_lz(int i, int s, int e) {
  if (st[i].lz.first || st[i].lz.second) {
    st[i].mx = st[i].lz.second + (e - s) * st[i].lz.first;
    if (s != e) {
      st[2 * i].lz = st[i].lz;
      st[2 * i + 1].lz = {
          st[i].lz.first,
          st[i].lz.second + ((s + e) / 2 + 1 - s) * st[i].lz.first};
    }
    st[i].lz = {0, 0};
  }
}
void upd(int i, int s, int e, int ss, int se, pair<int, int> ln) {
  upd_lz(i, s, e);
  if (s >= ss && e <= se)
    st[i].lz = ln, upd_lz(i, s, e);
  else {
    if ((s + e) / 2 < ss)
      upd(2 * i + 1, (s + e) / 2 + 1, e, ss, se, ln),
          upd_lz(2 * i, s, (s + e) / 2);
    else if ((s + e) / 2 >= se)
      upd(2 * i, s, (s + e) / 2, ss, se, ln),
          upd_lz(2 * i + 1, (s + e) / 2 + 1, e);
    else
      upd(2 * i, s, (s + e) / 2, ss, se, ln),
          upd(2 * i + 1, (s + e) / 2 + 1, e, ss, se,
              make_pair(ln.first,
                        ln.second + ln.first * ((s + e) / 2 + 1 - max(s, ss))));
    st[i].mx = max(st[2 * i].mx, st[2 * i + 1].mx);
  }
}
int qu(int i, int s, int e, int ss, int se) {
  upd_lz(i, s, e);
  if (s >= ss && e <= se)
    return st[i].mx;
  else if ((s + e) / 2 < ss)
    return qu(2 * i + 1, (s + e) / 2 + 1, e, ss, se);
  else if ((s + e) / 2 >= se)
    return qu(2 * i, s, (s + e) / 2, ss, se);
  else
    return max(qu(2 * i, s, (s + e) / 2, ss, se),
               qu(2 * i + 1, (s + e) / 2 + 1, e, ss, se));
}
int qu2(int i, int s, int e, int ss, int se, int val) {
  upd_lz(i, s, e);
  if (e < ss || s > se)
    return -1;
  else if (s >= ss && e <= se) {
    if (s == e && st[i].mx > val)
      return s;
    else if (st[i].mx <= val)
      return -1;
  } else if ((s + e) / 2 < ss)
    return qu2(2 * i + 1, (s + e) / 2 + 1, e, ss, se, val);
  else if ((s + e) / 2 >= se)
    return qu2(2 * i, s, (s + e) / 2, ss, se, val);
  int res = qu2(2 * i + 1, (s + e) / 2 + 1, e, ss, se, val);
  if (res != -1) return res;
  return qu2(2 * i, s, (s + e) / 2, ss, se, val);
}
int main() {
  scanf("%d%d", &N, &M);
  for (i = 1; i <= N; i++) {
    scanf("%d", &x);
    adj[x].push_back(i);
  }
  memset(lnk, -1, sizeof(lnk));
  par[0] = -1;
  dfs(0);
  dfs2(0);
  for (i = 1; i <= M; i++) {
    scanf("%d%d", &q[i].x, &q[i].y);
    q[i].id = i;
    q[i].y += dep[q[i].x];
  }
  sort(q + 1, q + M + 1,
       [](qur i, qur j) { return i.y == j.y ? i.x < j.x : i.y < j.y; });
  for (i = 1; i <= M; i++) {
    x = q[i].x, t = q[i].y;
    vector<pair<int, int> > chn;
    while (x != -1) {
      y = lnk[x];
      if (qu(1, 1, nxt, idx[y], idx[x]) > t) {
        int b = rev[qu2(1, 1, nxt, idx[y], idx[x], t)];
        if (x != b) chn.push_back({idx[b] + 1, idx[x]});
        break;
      }
      chn.push_back({idx[y], idx[x]});
      x = par[y];
    }
    if (chn.empty())
      ans[q[i].id] = q[i].y - dep[q[i].x];
    else if (x == -1) {
      int tt = q[i].y;
      ans[q[i].id] = tt + dep[q[i].x];
      reverse(chn.begin(), chn.end());
      for (auto v : chn) {
        upd(1, 1, nxt, v.first, v.second, make_pair(2, tt));
        tt += 2 * (v.second - v.first + 1);
      }
    } else {
      reverse(chn.begin(), chn.end());
      x = rev[chn[0].first];
      ans[q[i].id] = q[i].y + dep[q[i].x] - 2 * dep[x] + 2;
      int tt = q[i].y + 2;
      for (auto v : chn) {
        upd(1, 1, nxt, v.first, v.second, make_pair(2, tt));
        tt += 2 * (v.second - v.first + 1);
      }
    }
  }
  for (i = 1; i <= M; i++) printf("%d ", ans[i]);
  printf("\n");
  return 0;
}