#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
int tStamp;
struct Edge {
  int v, pre;
};
Edge e[100010];
int hh[100010], ecnt;
int L[100010], R[100010], fa[100010][20], dep[100010], vis[100010];
void dfs(int u, int pu) {
  vis[u] = 1;
  L[u] = ++tStamp;
  fa[u][0] = pu;
  for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  if (u == pu)
    dep[u] = 0;
  else
    dep[u] = dep[pu] + 1;
  g[dep[u]].push_back(tStamp);
  for (int i = hh[u]; i != -1; i = e[i].pre) {
    int v = e[i].v;
    if (v != pu) dfs(v, u);
  }
  R[u] = tStamp;
}
void addEdge(int u, int v) {
  e[ecnt].v = v;
  e[ecnt].pre = hh[u];
  hh[u] = ecnt++;
}
int getroot(int u, int dep) {
  for (int i = 0; i < 20; i++) {
    if (dep & (1 << i)) u = fa[u][i];
  }
  return u;
}
int get1(int num, int l) {
  int L = 0, R = g[num].size() - 1;
  if (g[num].size() == 0) return -1;
  while (L + 1 < R) {
    int mid = (L + R) >> 1;
    if (g[num][mid] >= l)
      R = mid;
    else
      L = mid;
  }
  if (g[num][L] >= l)
    return L;
  else if (g[num][R] >= l)
    return R;
  return -1;
}
int get2(int num, int l) {
  int L = 0, R = g[num].size() - 1;
  if (g[num].size() == 0) return -1;
  while (L + 1 < R) {
    int mid = (L + R) >> 1;
    if (g[num][mid] <= l)
      L = mid;
    else
      R = mid;
  }
  if (g[num][R] <= l)
    return R;
  else if (g[num][L] <= l)
    return L;
  return -1;
}
int vv[100010], pp[100010];
int faa[100010];
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    memset(hh, -1, sizeof(hh));
    ecnt = 0;
    for (int i = 1; i <= n; i++) {
      int fa;
      scanf("%d", &faa[i]);
      if (faa[i] != 0) addEdge(faa[i], i);
    }
    for (int i = 1; i <= 100000; i++) g[i].clear();
    tStamp = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
      if (faa[i] == 0) dfs(i, i);
    }
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) scanf("%d%d", &vv[i], &pp[i]);
    for (int i = 0; i < q; i++) {
      int v, p;
      v = vv[i];
      p = pp[i];
      if (dep[v] < p) {
        printf("0 ");
        continue;
      }
      int tfa = getroot(v, p);
      int tfa2 = getroot(v, p - 1);
      int l = L[tfa], r = R[tfa];
      int id1 = get1(p + dep[tfa], l);
      int id2 = get2(p + dep[tfa], r);
      int anss = id2 - id1 + 1;
      printf("%d ", anss - 1);
    }
    puts("");
  }
  return 0;
}

### Prompt

Please create a solution in Cpp 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;
vector<int> g[100010];
int tStamp;
struct Edge {
  int v, pre;
};
Edge e[100010];
int hh[100010], ecnt;
int L[100010], R[100010], fa[100010][20], dep[100010], vis[100010];
void dfs(int u, int pu) {
  vis[u] = 1;
  L[u] = ++tStamp;
  fa[u][0] = pu;
  for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  if (u == pu)
    dep[u] = 0;
  else
    dep[u] = dep[pu] + 1;
  g[dep[u]].push_back(tStamp);
  for (int i = hh[u]; i != -1; i = e[i].pre) {
    int v = e[i].v;
    if (v != pu) dfs(v, u);
  }
  R[u] = tStamp;
}
void addEdge(int u, int v) {
  e[ecnt].v = v;
  e[ecnt].pre = hh[u];
  hh[u] = ecnt++;
}
int getroot(int u, int dep) {
  for (int i = 0; i < 20; i++) {
    if (dep & (1 << i)) u = fa[u][i];
  }
  return u;
}
int get1(int num, int l) {
  int L = 0, R = g[num].size() - 1;
  if (g[num].size() == 0) return -1;
  while (L + 1 < R) {
    int mid = (L + R) >> 1;
    if (g[num][mid] >= l)
      R = mid;
    else
      L = mid;
  }
  if (g[num][L] >= l)
    return L;
  else if (g[num][R] >= l)
    return R;
  return -1;
}
int get2(int num, int l) {
  int L = 0, R = g[num].size() - 1;
  if (g[num].size() == 0) return -1;
  while (L + 1 < R) {
    int mid = (L + R) >> 1;
    if (g[num][mid] <= l)
      L = mid;
    else
      R = mid;
  }
  if (g[num][R] <= l)
    return R;
  else if (g[num][L] <= l)
    return L;
  return -1;
}
int vv[100010], pp[100010];
int faa[100010];
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    memset(hh, -1, sizeof(hh));
    ecnt = 0;
    for (int i = 1; i <= n; i++) {
      int fa;
      scanf("%d", &faa[i]);
      if (faa[i] != 0) addEdge(faa[i], i);
    }
    for (int i = 1; i <= 100000; i++) g[i].clear();
    tStamp = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
      if (faa[i] == 0) dfs(i, i);
    }
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) scanf("%d%d", &vv[i], &pp[i]);
    for (int i = 0; i < q; i++) {
      int v, p;
      v = vv[i];
      p = pp[i];
      if (dep[v] < p) {
        printf("0 ");
        continue;
      }
      int tfa = getroot(v, p);
      int tfa2 = getroot(v, p - 1);
      int l = L[tfa], r = R[tfa];
      int id1 = get1(p + dep[tfa], l);
      int id2 = get2(p + dep[tfa], r);
      int anss = id2 - id1 + 1;
      printf("%d ", anss - 1);
    }
    puts("");
  }
  return 0;
}

```