#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dblcmp(double d) {
  if (fabs(d) < eps) return 0;
  return d > eps ? 1 : -1;
}
struct treap {
  struct tnode {
    tnode *l, *r;
    int x, size, k;
    const int h;
    tnode(int a) : l(NULL), r(NULL), x(a), k(1), h(rand()), size(0) {}
    ~tnode() {
      delete l;
      delete r;
    }
    void upd() {
      size = k;
      if (l) size += l->size;
      if (r) size += r->size;
    }
  } * root;
  treap() : root(NULL) {}
  static tnode *zig(tnode *p) {
    tnode *t = p->l;
    p->l = t->r;
    t->r = p;
    p->upd();
    t->upd();
    return t;
  }
  static tnode *zag(tnode *p) {
    tnode *t = p->r;
    p->r = t->l;
    t->l = p;
    p->upd();
    t->upd();
    return t;
  }
  static tnode *merge(tnode *l, tnode *r) {
    if (l == NULL) return r;
    if (r == NULL) return l;
    if (l->h < r->h) {
      l->r = merge(l->r, r);
      l->upd();
      return l;
    }
    r->l = merge(l, r->l);
    r->upd();
    return r;
  }
  static tnode *insert(tnode *p, int a) {
    if (p == NULL)
      p = new tnode(a);
    else if (p->x == a) {
      p->k++;
    } else if (a < p->x) {
      p->l = insert(p->l, a);
      if (p->l->h < p->h) p = zig(p);
    } else {
      p->r = insert(p->r, a);
      if (p->r->h < p->h) p = zag(p);
    }
    p->upd();
    return p;
  }
  void insert(int a) { root = insert(root, a); }
  static tnode *remove(tnode *p, int a) {
    if (p == NULL)
      return NULL;
    else if (p->x == a) {
      p->k--;
      if (p->k == 0) {
        tnode *t = p;
        p = merge(p->l, p->r);
        t->l = t->r = NULL;
        delete t;
      }
    } else if (a < p->x) {
      p->l = remove(p->l, a);
    } else {
      p->r = remove(p->r, a);
    }
    if (p) p->upd();
    return p;
  }
  void remove(int a) { root = remove(root, a); }
  static int count(tnode *p, int a) {
    int ans;
    if (p == NULL)
      return 0;
    else if (p->x <= a) {
      ans = p->k;
      if (p->l) ans += p->l->size;
      ans += count(p->r, a);
      return ans;
    } else
      return count(p->l, a);
  }
  int count(int a) { return count(root, a - 1); }
  void cc() {
    delete root;
    root = NULL;
  }
};
treap gao[333333];
vector<int> g[211111];
int f[211111][18];
int n;
int z[211111];
int l[211111], r[211111], st, d[211111];
void dfs(int x, int fa, int de) {
  int i;
  l[x] = ++st;
  d[x] = de;
  for (i = 1; i <= 17; i++) {
    if (f[x][i - 1] != -1) {
      f[x][i] = f[f[x][i - 1]][i - 1];
    }
  }
  z[x] = 1;
  for (int i = 0; i < (int)((g[x]).size()); i++) {
    f[g[x][i]][0] = x;
    dfs(g[x][i], x, de + 1);
    z[x] += z[g[x][i]];
  }
  r[x] = ++st;
}
int main() {
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> k;
    g[k].push_back(i);
  }
  memset((f), -1, sizeof(f));
  dfs(0, -1, 0);
  for (i = 1; i <= n; i++) {
    gao[d[i]].insert(r[i]);
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    for (i = y, j = 0; i; i >>= 1, j++) {
      if (i & 1) x = f[x][j];
      if (x == -1) break;
    }
    if (x == -1 || x == 0) {
      puts("0");
      continue;
    }
    int t = d[x];
    t += y;
    int a1 = gao[t].count(r[x]);
    a1 -= gao[t].count(l[x]);
    printf("%d ", a1 - 1);
  }
  puts("");
  return 0;
}

### Prompt

Please formulate a cpp solution to the following problem:

Polycarpus got hold of a family relationship tree. The tree describes family relationships of n people, numbered 1 through n. Each person in the tree has no more than one parent.

Let's call person a a 1-ancestor of person b, if a is the parent of b.

Let's call person a a k-ancestor (k > 1) of person b, if person b has a 1-ancestor, and a is a (k - 1)-ancestor of b's 1-ancestor. 

Family relationships don't form cycles in the found tree. In other words, there is no person who is his own ancestor, directly or indirectly (that is, who is an x-ancestor for himself, for some x, x > 0).

Let's call two people x and y (x ≠ y) p-th cousins (p > 0), if there is person z, who is a p-ancestor of x and a p-ancestor of y.

Polycarpus wonders how many counsins and what kinds of them everybody has. He took a piece of paper and wrote m pairs of integers vi, pi. Help him to calculate the number of pi-th cousins that person vi has, for each pair vi, pi.

Input

The first input line contains a single integer n (1 ≤ n ≤ 105) — the number of people in the tree. The next line contains n space-separated integers r1, r2, ..., rn, where ri (1 ≤ ri ≤ n) is the number of person i's parent or 0, if person i has no parent. It is guaranteed that family relationships don't form cycles.

The third line contains a single number m (1 ≤ m ≤ 105) — the number of family relationship queries Polycarus has. Next m lines contain pairs of space-separated integers. The i-th line contains numbers vi, pi (1 ≤ vi, pi ≤ n).

Output

Print m space-separated integers — the answers to Polycarpus' queries. Print the answers to the queries in the order, in which the queries occur in the input.

Examples

Input

6
0 1 1 0 4 4
7
1 1
1 2
2 1
2 2
4 1
5 1
6 1


Output

0 0 1 0 0 1 1 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dblcmp(double d) {
  if (fabs(d) < eps) return 0;
  return d > eps ? 1 : -1;
}
struct treap {
  struct tnode {
    tnode *l, *r;
    int x, size, k;
    const int h;
    tnode(int a) : l(NULL), r(NULL), x(a), k(1), h(rand()), size(0) {}
    ~tnode() {
      delete l;
      delete r;
    }
    void upd() {
      size = k;
      if (l) size += l->size;
      if (r) size += r->size;
    }
  } * root;
  treap() : root(NULL) {}
  static tnode *zig(tnode *p) {
    tnode *t = p->l;
    p->l = t->r;
    t->r = p;
    p->upd();
    t->upd();
    return t;
  }
  static tnode *zag(tnode *p) {
    tnode *t = p->r;
    p->r = t->l;
    t->l = p;
    p->upd();
    t->upd();
    return t;
  }
  static tnode *merge(tnode *l, tnode *r) {
    if (l == NULL) return r;
    if (r == NULL) return l;
    if (l->h < r->h) {
      l->r = merge(l->r, r);
      l->upd();
      return l;
    }
    r->l = merge(l, r->l);
    r->upd();
    return r;
  }
  static tnode *insert(tnode *p, int a) {
    if (p == NULL)
      p = new tnode(a);
    else if (p->x == a) {
      p->k++;
    } else if (a < p->x) {
      p->l = insert(p->l, a);
      if (p->l->h < p->h) p = zig(p);
    } else {
      p->r = insert(p->r, a);
      if (p->r->h < p->h) p = zag(p);
    }
    p->upd();
    return p;
  }
  void insert(int a) { root = insert(root, a); }
  static tnode *remove(tnode *p, int a) {
    if (p == NULL)
      return NULL;
    else if (p->x == a) {
      p->k--;
      if (p->k == 0) {
        tnode *t = p;
        p = merge(p->l, p->r);
        t->l = t->r = NULL;
        delete t;
      }
    } else if (a < p->x) {
      p->l = remove(p->l, a);
    } else {
      p->r = remove(p->r, a);
    }
    if (p) p->upd();
    return p;
  }
  void remove(int a) { root = remove(root, a); }
  static int count(tnode *p, int a) {
    int ans;
    if (p == NULL)
      return 0;
    else if (p->x <= a) {
      ans = p->k;
      if (p->l) ans += p->l->size;
      ans += count(p->r, a);
      return ans;
    } else
      return count(p->l, a);
  }
  int count(int a) { return count(root, a - 1); }
  void cc() {
    delete root;
    root = NULL;
  }
};
treap gao[333333];
vector<int> g[211111];
int f[211111][18];
int n;
int z[211111];
int l[211111], r[211111], st, d[211111];
void dfs(int x, int fa, int de) {
  int i;
  l[x] = ++st;
  d[x] = de;
  for (i = 1; i <= 17; i++) {
    if (f[x][i - 1] != -1) {
      f[x][i] = f[f[x][i - 1]][i - 1];
    }
  }
  z[x] = 1;
  for (int i = 0; i < (int)((g[x]).size()); i++) {
    f[g[x][i]][0] = x;
    dfs(g[x][i], x, de + 1);
    z[x] += z[g[x][i]];
  }
  r[x] = ++st;
}
int main() {
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> k;
    g[k].push_back(i);
  }
  memset((f), -1, sizeof(f));
  dfs(0, -1, 0);
  for (i = 1; i <= n; i++) {
    gao[d[i]].insert(r[i]);
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    for (i = y, j = 0; i; i >>= 1, j++) {
      if (i & 1) x = f[x][j];
      if (x == -1) break;
    }
    if (x == -1 || x == 0) {
      puts("0");
      continue;
    }
    int t = d[x];
    t += y;
    int a1 = gao[t].count(r[x]);
    a1 -= gao[t].count(l[x]);
    printf("%d ", a1 - 1);
  }
  puts("");
  return 0;
}

```