#include <bits/stdc++.h>
using namespace std;
void itval(istream_iterator<string> it) {}
template <typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  itval(++it, args...);
}
const long long int MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {
  cout << x << "\n";
}
template <typename T>
inline void printvec(T x) {
  for (auto a : x) cout << a << ' ';
  cout << '\n';
}
struct custom {
  bool operator()(const pair<long long int, pair<int, int> > &p1,
                  const pair<long long int, pair<int, int> > &p2) const {
    return p1.first < p2.first;
  }
};
long long int get_pow(long long int a, long long int b, long long int M = MOD) {
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }
  return res;
}
const long long int N = 1e5 + 5, inf = 2e18;
std::vector<int> adj[N], dist[N];
int dep[N], up[N][20], l = 19, tin[N], tout[N], timer;
bool vis[N];
void dfs(int v, int p, int h) {
  tin[v] = ++timer;
  up[v][0] = p;
  dep[v] = h;
  dist[h].push_back(timer);
  for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
  for (int u : adj[v]) {
    if (u != p) {
      dfs(u, v, h + 1);
    }
  }
  tout[v] = ++timer;
}
void solve() {
  int n, x, y;
  vector<int> roots;
  cin >> n;
  for (long long int i = (long long int)1; i < (long long int)(n + 1); i++) {
    cin >> x;
    if (x == 0) {
      roots.push_back(i);
    } else {
      adj[i].push_back(x);
      adj[x].push_back(i);
    }
  }
  for (auto i : roots) dfs(i, 0, 0);
  int q, v, p, d, cnt, low, c;
  cin >> q;
  for (long long int i = (long long int)0; i < (long long int)(n); i++) {
    if (!dist[i].empty()) sort(dist[i].begin(), dist[i].end());
  }
  for (long long int i = (long long int)0; i < (long long int)(q); i++) {
    cin >> v >> p;
    c = v;
    for (int j = l; j >= 0; j--) {
      if ((1 << j) <= p) {
        p -= (1 << j);
        c = up[c][j];
      }
    }
    if (c == 0) {
      cout << 0 << ' ';
      continue;
    }
    d = dep[v];
    cnt = upper_bound(dist[d].begin(), dist[d].end(), tout[c]) -
          lower_bound(dist[d].begin(), dist[d].end(), tin[c]) - 1;
    cout << cnt << ' ';
  }
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  clock_t z = clock();
  for (long long int tes = (long long int)0; tes < (long long int)(test);
       tes++) {
    solve();
  }
  fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC),
      fflush(stderr);
  return 0;
}

### Prompt

Generate 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;
void itval(istream_iterator<string> it) {}
template <typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  itval(++it, args...);
}
const long long int MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {
  cout << x << "\n";
}
template <typename T>
inline void printvec(T x) {
  for (auto a : x) cout << a << ' ';
  cout << '\n';
}
struct custom {
  bool operator()(const pair<long long int, pair<int, int> > &p1,
                  const pair<long long int, pair<int, int> > &p2) const {
    return p1.first < p2.first;
  }
};
long long int get_pow(long long int a, long long int b, long long int M = MOD) {
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }
  return res;
}
const long long int N = 1e5 + 5, inf = 2e18;
std::vector<int> adj[N], dist[N];
int dep[N], up[N][20], l = 19, tin[N], tout[N], timer;
bool vis[N];
void dfs(int v, int p, int h) {
  tin[v] = ++timer;
  up[v][0] = p;
  dep[v] = h;
  dist[h].push_back(timer);
  for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1];
  for (int u : adj[v]) {
    if (u != p) {
      dfs(u, v, h + 1);
    }
  }
  tout[v] = ++timer;
}
void solve() {
  int n, x, y;
  vector<int> roots;
  cin >> n;
  for (long long int i = (long long int)1; i < (long long int)(n + 1); i++) {
    cin >> x;
    if (x == 0) {
      roots.push_back(i);
    } else {
      adj[i].push_back(x);
      adj[x].push_back(i);
    }
  }
  for (auto i : roots) dfs(i, 0, 0);
  int q, v, p, d, cnt, low, c;
  cin >> q;
  for (long long int i = (long long int)0; i < (long long int)(n); i++) {
    if (!dist[i].empty()) sort(dist[i].begin(), dist[i].end());
  }
  for (long long int i = (long long int)0; i < (long long int)(q); i++) {
    cin >> v >> p;
    c = v;
    for (int j = l; j >= 0; j--) {
      if ((1 << j) <= p) {
        p -= (1 << j);
        c = up[c][j];
      }
    }
    if (c == 0) {
      cout << 0 << ' ';
      continue;
    }
    d = dep[v];
    cnt = upper_bound(dist[d].begin(), dist[d].end(), tout[c]) -
          lower_bound(dist[d].begin(), dist[d].end(), tin[c]) - 1;
    cout << cnt << ' ';
  }
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  clock_t z = clock();
  for (long long int tes = (long long int)0; tes < (long long int)(test);
       tes++) {
    solve();
  }
  fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC),
      fflush(stderr);
  return 0;
}

```