#include <bits/stdc++.h>
using namespace std;
struct FastIO {
  inline FastIO& operator>>(int& x) {
    x = 0;
    char f = 0, ch = getchar();
    while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
    while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
    return x = (f ? -x : x), *this;
  }
  inline FastIO& operator>>(long long& x) {
    x = 0;
    char f = 0, ch = getchar();
    while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
    while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
    return x = (f ? -x : x), *this;
  }
  inline FastIO& operator>>(double& x) {
    x = 0;
    char f = 0, ch = getchar();
    double d = 0.1;
    while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
    while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
    if (ch == '.') {
      ch = getchar();
      while (ch <= '9' && ch >= '0')
        x += d * (ch ^ 48), d *= 0.1, ch = getchar();
    }
    return x = (f ? -x : x), *this;
  }
} read;
void file() {}
unsigned int log2(int x) {
  unsigned int ret;
  __asm__ __volatile__("bsrl %1, %%eax" : "=a"(ret) : "m"(x));
  return ret;
}
inline long long mul(long long a, long long b) {
  long long d = (long long)(a * (double)b / 998244353 + 0.5);
  long long ret = a * b - d * 998244353;
  if (ret < 0) ret += 998244353;
  return ret;
}
int n;
struct hh {
  int t, nxt;
} edge[1000020 << 1];
int head[1000020], ecnt;
void make_edge(int f, int t) {
  edge[++ecnt] = (hh){t, head[f]};
  head[f] = ecnt;
  edge[++ecnt] = (hh){f, head[t]};
  head[t] = ecnt;
}
int dep[1000020], size[1000020], son[1000020], fa[1000020];
void dfs1(int x, int fa) {
  dep[x] = dep[fa] + 1;
  size[x] = 1;
  for (int i = head[x]; i; i = edge[i].nxt)
    if (edge[i].t != fa) {
      dfs1(edge[i].t, x);
      size[x] += size[edge[i].t];
      if (size[edge[i].t] > size[son[x]]) son[x] = edge[i].t;
    }
}
map<string, int> S[1000020];
int sum[1000020];
string a[1000020];
bool s[1000020];
void add(string s, int d, int t) {
  if (!S[d][s]) ++sum[d];
  S[d][s] += t;
  if (!S[d][s]) --sum[d];
}
void add(int x, int fa, int t) {
  add(a[x], dep[x], t);
  for (int i = head[x]; i; i = edge[i].nxt)
    if (edge[i].t != fa && !s[edge[i].t]) add(edge[i].t, x, t);
}
vector<pair<int, int> > q[1000020];
int ans[1000020];
void dfs2(int x, int fa, bool keep) {
  for (int i = head[x]; i; i = edge[i].nxt)
    if (edge[i].t != fa && edge[i].t != son[x]) dfs2(edge[i].t, x, 0);
  s[son[x]] = 1;
  if (son[x]) dfs2(son[x], x, 1);
  add(x, fa, 1);
  for (int i = 0; i < (int)q[x].size(); i++)
    ans[q[x][i].first] = sum[q[x][i].second];
  s[son[x]] = 0;
  if (!keep) add(x, fa, -1);
}
int main() {
  file();
  int i, x, y, m;
  read >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    read >> fa[i];
    if (x = fa[i]) make_edge(x, i);
  }
  for (i = 1; i <= n; i++)
    if (!fa[i]) dfs1(i, 0);
  read >> m;
  for (i = 1; i <= m; i++) {
    read >> x >> y;
    q[x].push_back(make_pair(i, dep[x] + y));
  }
  for (i = 1; i <= n; i++)
    if (!fa[i]) dfs2(i, 0, 0);
  for (i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
