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
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void r1(T &x) {
  x = 0;
  char c(getchar());
  int f(1);
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  x *= f;
}
const int maxn = 1e5 + 5;
const int maxm = maxn << 1;
typedef int room[maxn];
int n, m;
map<string, int> vis;
int num(0), v[maxn];
int dep[maxn];
struct Node {
  int l, r, id;
} t[maxn * 21];
int stlen(0);
set<int> st[maxn * 21];
struct Seg {
  int tot;
  Seg() : tot(0) {}
  void Insert(int &p, int l, int r, int pos, int v) {
    if (!p) p = ++tot;
    if (l == r)
      return !t[p].id ? t[p].id = ++stlen : 0, st[t[p].id].insert(v), void();
    if (pos <= ((l + r) >> 1))
      Insert(t[p].l, l, ((l + r) >> 1), pos, v);
    else
      Insert(t[p].r, ((l + r) >> 1) + 1, r, pos, v);
  }
  void merge(int &p, int v, int l, int r) {
    if (!p || !v) return p = p + v, void();
    if (l == r) {
      if (!t[p].id)
        t[p].id = ++stlen;
      else {
        if (st[t[p].id].size() < st[t[v].id].size())
          swap(st[t[p].id], st[t[v].id]);
        for (set<int>::iterator it = st[t[v].id].begin();
             it != st[t[v].id].end(); ++it)
          st[t[p].id].insert(*it);
        st[t[v].id].clear();
      }
      return;
    }
    merge(t[p].l, t[v].l, l, ((l + r) >> 1)),
        merge(t[p].r, t[v].r, ((l + r) >> 1) + 1, r);
  }
  int ask(int p, int l, int r, int pos) {
    if (!p) return 0;
    if (l == r) return st[t[p].id].size();
    if (pos <= ((l + r) >> 1))
      return ask(t[p].l, l, ((l + r) >> 1), pos);
    else
      return ask(t[p].r, ((l + r) >> 1) + 1, r, pos);
  }
} T;
struct Edge {
  int to, next;
} edg[maxn << 1];
int head[maxn], cnt;
void add(int u, int v) { edg[++cnt] = (Edge){v, head[u]}, head[u] = cnt; }
struct Query {
  int dep, id;
  Query(int a, int b) : dep(a), id(b) {}
};
vector<Query> vc[maxn];
int ans[maxn];
int rt[maxn];
void dfs(int p, int f) {
  T.Insert(rt[p], 1, n, dep[p], v[p]);
  for (int i = head[p]; i; i = edg[i].next) {
    int to = edg[i].to;
    if (to == f) continue;
    dfs(to, p);
    T.merge(rt[p], rt[to], 1, n);
  }
  for (vector<Query>::iterator it = vc[p].begin(); it != vc[p].end(); ++it)
    ans[it->id] = T.ask(rt[p], 1, n, it->dep);
}
void dfs1(int p, int dep1, int f) {
  dep[p] = dep1;
  for (int i = head[p]; i; i = edg[i].next) {
    int to = edg[i].to;
    if (to == f) continue;
    dfs1(to, dep1 + 1, p);
  }
}
signed main() {
  int i, j;
  r1(n);
  for (i = 1; i <= n; ++i) {
    int x;
    string s;
    cin >> s;
    r1(x);
    if (vis[s])
      v[i] = vis[s];
    else
      vis[s] = v[i] = ++num;
    add(x, i);
  }
  r1(m);
  dfs1(0, 0, 0);
  for (i = 1; i <= m; ++i) {
    int x, k;
    r1(x), r1(k);
    if (dep[x] + k <= n) vc[x].push_back(Query(dep[x] + k, i));
  }
  dfs(0, 0);
  for (i = 1; i <= m; ++i) printf("%d\n", ans[i]);
  return 0;
}
