Problem: Polycarpus got hold of a family tree. The found tree describes the family relations of n people, numbered from 1 to n. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique.

We call the man with a number a a 1-ancestor of the man with a number b, if the man with a number a is a direct ancestor of the man with a number b.

We call the man with a number a a k-ancestor (k > 1) of the man with a number b, if the man with a number b has a 1-ancestor, and the man with a number a is a (k - 1)-ancestor of the 1-ancestor of the man with a number b.

In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an x-ancestor of himself, for some x, x > 0).

We call a man with a number a the k-son of the man with a number b, if the man with a number b is a k-ancestor of the man with a number a.

Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote m pairs of numbers vi, ki. Help him to learn for each pair vi, ki the number of distinct names among all names of the ki-sons of the man with number vi.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 105) — the number of people in the tree. Next n lines contain the description of people in the tree. The i-th line contains space-separated string si and integer ri (0 ≤ ri ≤ n), where si is the name of the man with a number i, and ri is either the number of the direct ancestor of the man with a number i or 0, if the man with a number i has no direct ancestor. 

The next line contains a single integer m (1 ≤ m ≤ 105) — the number of Polycarpus's records. Next m lines contain space-separated pairs of integers. The i-th line contains integers vi, ki (1 ≤ vi, ki ≤ n).

It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters.

Output

Print m whitespace-separated integers — the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input.

Examples

Input

6
pasha 0
gerald 1
gerald 1
valera 2
igor 3
olesya 1
5
1 1
1 2
1 3
3 1
6 1


Output

2
2
0
1
0


Input

6
valera 0
valera 1
valera 1
gerald 0
valera 4
kolya 4
7
1 1
1 2
2 1
2 2
4 1
5 1
6 1


Output

1
0
0
0
2
0
0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = numeric_limits<long long>::max();
const long long NEGINF = numeric_limits<long long>::min();
template <typename T>
void print(T t) {
  cout << t << " ";
}
template <typename T, typename... Args>
void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}
template <class T>
void print(vector<T> &list) {
  for (auto &x : list) cout << x << " ";
}
template <class T>
void read(vector<T> &list, long long n) {
  list.clear(), list.resize(n);
  for (auto &x : list) cin >> x;
}
long long len_of_number(long long n) {
  long long len(0);
  while (n != 0) {
    ++len;
    n /= 10;
  }
  return len;
}
long long sum_of_digits(long long n) {
  long long sod = 0;
  while (n != 0) {
    sod += (n % 10);
    n /= 10;
  }
  return sod;
}
bool isPalindrome(string str) {
  long long n = str.length();
  for (long long i = (0); i <= (n / 2); i++)
    if (str[i] != str[n - i - 1]) return false;
  return true;
}
long long fast_exp(long long x, long long i) {
  long long ans = 1;
  while (i > 0) {
    if (i & 1) ans = (ans * x);
    i >>= 1;
    x = (x * x);
  }
  return ans;
}
long long fast_exp(long long x, long long i, long long mod) {
  long long ans = 1;
  while (i > 0) {
    if (i & 1) ans = (ans * x) % mod;
    i >>= 1;
    x = (x * x) % mod;
  }
  return ans;
}
long long modInverse(long long x, long long mod) {
  return fast_exp(x, mod - 2, mod);
}
long long egcd_x, egcd_y, egcd_d;
void egcd(long long a, long long b) {
  if (b == 0) {
    egcd_x = 1, egcd_y = 0;
    egcd_d = a;
    return;
  }
  egcd(b, a % b);
  long long temp = egcd_x;
  egcd_x = egcd_y;
  egcd_y = temp - (a / b) * egcd_y;
}
long long extended_gcd(long long a, long long b) {
  if (a >= b) {
    egcd(a, b);
    return ((egcd_x % b) + b) % b;
  } else {
    egcd(b, a);
    return ((egcd_y % b) + b) % b;
  }
}
long long gcd(long long x, long long y) {
  long long t;
  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}
vector<long long> lp, pr;
void genPrimes(long long n) {
  lp.resize(n + 1, 0);
  long long size = 0;
  for (long long i = (2); i <= (n); i++) {
    if (!lp[i]) {
      lp[i] = i;
      pr.push_back(i);
      ++size;
    }
    for (long long j = 0; j < size && pr[j] <= lp[i] && (i * pr[j]) <= n; ++j)
      lp[i * pr[j]] = pr[j];
  }
}
bool isPrime(long long n) {
  long long root = sqrt(n);
  for (long long i = (2); i <= (root); i++)
    if (n % i == 0) return 0;
  return 1;
}
vector<long long> fact, invFact;
void genFact(long long n, long long M) {
  fact.resize(n + 1), invFact.resize(n + 1);
  fact[0] = fact[1] = invFact[0] = invFact[1] = 1;
  for (long long i = (2); i <= (n); i++) {
    fact[i] = (fact[i - 1] * i) % M;
    invFact[i] = modInverse(fact[i], M);
  }
}
long long ncr(long long n, long long r, long long M) {
  return (((fact[n] * invFact[n - r]) % M) * invFact[r] % M);
}
long long nc2(long long a) { return (a * (a - 1)) / 2; }
vector<long long> cnt;
string groupAndCnt(string str) {
  string a = "";
  long long prev_count = 0;
  char prev_char = '~';
  for (auto &c : str) {
    if (c != prev_char) {
      if (prev_count) {
        a += prev_char;
        cnt.push_back(prev_count);
      }
      prev_count = 1;
      prev_char = c;
    } else
      ++prev_count;
  }
  cnt.push_back(prev_count);
  a += prev_char;
  return a;
}
long long firstValEG(vector<long long>::iterator a,
                     vector<long long>::iterator b, long long c) {
  auto it = lower_bound(a, b, c);
  if (it == b) return -1;
  return it - a;
}
long long lastValEL(vector<long long>::iterator a,
                    vector<long long>::iterator b, long long c) {
  long long lo = 0, hi = distance(a, b) - 1, m;
  while (lo < hi) {
    m = (lo) + ((hi) - (lo) + 1) / 2;
    if (*(a + m) > c)
      hi = m - 1;
    else
      lo = m;
  }
  return ((*(a + lo)) <= c) ? lo : -1;
}
long long countInRange(vector<long long>::iterator a,
                       vector<long long>::iterator b, long long c,
                       long long d) {
  long long e = firstValEG(a, b, c);
  if (e == -1) return 0;
  return (e == -1) ? 0 : (lastValEL(a, b, d) - e + 1);
}
vector<long long> pset, pset_size;
void initSet(long long _size) {
  pset.resize(_size);
  pset_size.resize(_size);
  for (long long i = (0); i <= (_size - 1); i++) pset[i] = i, pset_size[i] = 1;
}
long long findSet(long long i) {
  return (pset[i] == i) ? i : (pset[i] = findSet(pset[i]));
}
void unionSet(long long i, long long j) {
  long long p1 = findSet(i);
  long long p2 = findSet(j);
  pset[p1] = p2;
  if (p1 != p2) {
    pset_size[p2] += pset_size[p1];
  }
}
bool isSameSet(long long i, long long j) { return findSet(i) == findSet(j); }
vector<vector<long long> > adjList;
void getEdges(long long n, long long m) {
  long long a, b;
  adjList.clear();
  adjList.resize(n + 1);
  for (long long i = (1); i <= (m); i++) {
    cin >> a >> b;
    adjList[a].push_back(b), adjList[b].push_back(a);
  }
}
void printGraph(long long a, long long b, vector<vector<long long> > &graph) {
  for (long long i = (a); i <= (b); i++) {
    print(i, ": ");
    for (auto &x : graph[i]) {
      print(x, ",");
    }
    cout << "\n";
  }
}
long long n, x;
long long TIME = 0;
vector<long long> depth;
const long long MAXN = 100010, LOGN = 20;
long long parent[MAXN][LOGN];
long long inTime[MAXN], outTime[MAXN];
vector<long long> nodes[MAXN];
void dfs(long long r) {
  inTime[r] = TIME++;
  nodes[depth[r]].push_back(inTime[r]);
  for (auto &child : adjList[r]) {
    depth[child] = 1 + depth[r];
    parent[child][0] = r;
    for (long long i = 1; i < LOGN; i++) {
      parent[child][i] = parent[parent[child][i - 1]][i - 1];
    }
    dfs(child);
  }
  outTime[r] = TIME;
}
long long getParent(long long v, long long p) {
  if (p > depth[v] - 1) return -1;
  for (long long i = 0; i < LOGN; i++) {
    if (p & (1 << i)) v = parent[v][i];
  }
  return v;
}
inline void solve(long long _T) {
  cin >> n;
  adjList.resize(n + 1);
  for (long long i = (1); i <= (n); i++) {
    cin >> x;
    adjList[x].push_back(i);
  }
  depth.resize(n + 1, 0);
  dfs(0);
  long long m;
  cin >> m;
  long long v, p;
  for (long long i = (1); i <= (m); i++) {
    cin >> v >> p;
    v = getParent(v, p);
    if (v == -1) {
      cout << 0 << " ";
      continue;
    }
    cout << countInRange(nodes[depth[v] + p].begin(), nodes[depth[v] + p].end(),
                         inTime[v], outTime[v]) -
                1
         << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long test_cases;
  test_cases = 1;
  for (long long T = (0); T <= (test_cases - 1); T++) {
    solve(T);
  }
  return 0;
}