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;
using namespace rel_ops;
using ll = int64_t;
using Pii = pair<int, int>;
using ull = uint64_t;
using Vi = vector<int>;
void run();
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  run();
  return 0;
}
int uplg(int n) { return 32 - __builtin_clz(n); }
int uplg(ll n) { return 64 - __builtin_clzll(n); }
struct SegmentTree {
  using T = int;
  static constexpr T ID = INT_MIN;
  struct Node {
    T extra{ID};
    T great{INT_MIN};
    void init(T first, int) { great = first; }
    void merge(const Node& R) { great = max(great, R.great); }
    T apply(T first, int begin, int size) {
      extra = max(extra, first);
      great = max(great, first + (begin + size - 1) * 2);
      return first;
    }
  };
  vector<Node> V;
  int len;
  SegmentTree(int n = 0, T def = ID) { init(n, def); }
  void init(int n, T def) {
    for (len = 1; len < n; len *= 2)
      ;
    V.assign(len * 2, {});
    for (int i = (len); i < (len + n); i++) V[i].init(def, 1);
    for (int i = len - 1; i > 0; i--) update(i);
  }
  int getChild(int i, int j) { return i * 2 + j; }
  int L(int i) { return getChild(i, 0); }
  int R(int i) { return getChild(i, 1); }
  void update(int i) {
    int a = L(i), b = R(i);
    V[i] = {};
    V[i].merge(V[a]);
    V[i].merge(V[b]);
  }
  void push(int i, int begin, int size) {
    int e = V[i].extra;
    if (e != ID) {
      e = V[L(i)].apply(e, begin, size / 2);
      V[R(i)].apply(e, begin + size / 2, size / 2);
      V[i].extra = ID;
    }
  }
  T modify(int vBegin, int vEnd, T first, int i = 1, int begin = 0,
           int end = -1) {
    if (end < 0) end = len;
    if (vEnd <= begin || end <= vBegin) return first;
    if (vBegin <= begin && end <= vEnd) {
      return V[i].apply(first, begin, end - begin);
    }
    int mid = (begin + end) / 2;
    push(i, begin, end - begin);
    first = modify(vBegin, vEnd, first, L(i), begin, mid);
    first = modify(vBegin, vEnd, first, R(i), mid, end);
    update(i);
    return first;
  }
  Node query(int vBegin, int vEnd, int i = 1, int begin = 0, int end = -1) {
    if (end < 0) end = len;
    if (vEnd <= begin || end <= vBegin) return {};
    if (vBegin <= begin && end <= vEnd) return V[i];
    int mid = (begin + end) / 2;
    push(i, begin, end - begin);
    Node first = query(vBegin, vEnd, L(i), begin, mid);
    first.merge(query(vBegin, vEnd, R(i), mid, end));
    return first;
  }
  int search(int vBegin, int vEnd, int value, int i = 1, int begin = 0,
             int end = -1) {
    if (end < 0) end = len;
    if (vEnd <= begin || end <= vBegin) return begin;
    if (vBegin <= begin && end <= vEnd) {
      if (V[i].great < value) return begin;
      if (begin + 1 == end) return end;
    }
    int mid = (begin + end) / 2;
    push(i, begin, end - begin);
    int ind = search(vBegin, vEnd, value, R(i), mid, end);
    if (ind > mid) return ind;
    return search(vBegin, vEnd, value, L(i), begin, mid);
  }
};
constexpr int INF = 1.6e9;
struct Vert {
  Vi E;
  int parent, size, depth, chain, chainPos;
  ;
};
struct Chain {
  Vi verts;
  SegmentTree tree;
  ;
};
struct Query {
  int from, time, ans;
  ;
};
vector<Vert> G;
vector<Chain> chains;
vector<Query> queries;
void hld(int i, int parent = -1, int d = 0) {
  int h = -1;
  G[i].parent = parent;
  G[i].depth = d;
  G[i].size = 1;
  for (auto& e : (G[i].E))
    if (e != parent) {
      hld(e, i, d + 1);
      G[i].size += G[e].size;
      if (h < 0 || G[h].size < G[e].size) h = e;
    }
  if (h < 0) {
    h = int((chains).size());
    chains.emplace_back();
  } else {
    h = G[h].chain;
  }
  chains[h].verts.push_back(i);
  G[i].chain = h;
  if (parent < 0) {
    for (auto& p : (chains)) {
      reverse((p.verts).begin(), (p.verts).end());
      for (int j = (0); j < (int((p.verts).size())); j++)
        G[p.verts[j]].chainPos = j;
      p.tree.init(int((p.verts).size()), 0);
    }
  }
}
int rootTime(Query& q) { return q.time + G[q.from].depth; }
void handle(Query& q) {
  int to = q.from;
  int t = rootTime(q);
  while (true) {
    auto& chain = chains[G[to].chain];
    int i = chain.tree.search(0, G[to].chainPos + 1, t);
    if (i > 0) {
      to = chain.verts[i - 1];
      break;
    }
    to = G[chain.verts[0]].parent;
  }
  q.ans = q.time + (G[q.from].depth - G[to].depth) * 2;
  int v = q.from;
  t = q.time + G[q.from].depth - G[to].depth;
  while (true) {
    auto& chain = chains[G[v].chain];
    bool finalChain = (G[to].chain == G[v].chain);
    int begin = (finalChain ? G[to].chainPos + 1 : 0);
    int end = G[v].chainPos + 1;
    if (begin < end) {
      int time = t + 2 * G[chain.verts[0]].depth - G[to].depth;
      chain.tree.modify(begin, end, time - 1);
    }
    if (finalChain) break;
    v = G[chain.verts[0]].parent;
  }
}
void solve() {
  hld(0);
  Vi sorted(int((queries).size()));
  iota((sorted).begin(), (sorted).end(), 0);
  sort((sorted).begin(), (sorted).end(), [](int l, int r) {
    return make_pair(rootTime(queries[l]), queries[l].from) <
           make_pair(rootTime(queries[r]), queries[r].from);
  });
  chains[G[0].chain].tree.modify(0, 1, INF);
  for (auto& i : (sorted)) handle(queries[i]);
}
void run() {
  int n, m;
  cin >> n >> m;
  n++;
  G.resize(n);
  for (int i = (1); i < (n); i++) {
    int j;
    cin >> j;
    G[i].E.push_back(j);
    G[j].E.push_back(i);
  }
  queries.resize(m);
  for (auto& q : (queries)) cin >> q.from >> q.time;
  solve();
  for (auto& q : (queries)) cout << q.ans << ' ';
  cout << endl;
}