#include <bits/stdc++.h>
using namespace std;
string val[100007];
unordered_map<string, int> mp[100007];
vector<int> adj[100007];
vector<pair<int, int> > qq[100007];
int sub[100007], lvl[100007];
int cnt[100007];
int ans[100007];
bool mark[100007];
int st[100007], en[100007], rev[100007];
int Time;
void dfs(int u, int pre) {
  st[u] = ++Time;
  lvl[u] = lvl[pre] + 1;
  rev[Time] = u;
  sub[u]++;
  for (auto v : adj[u]) {
    dfs(v, u);
    sub[u] += sub[v];
  }
  en[u] = Time;
}
void sack(int u, bool keep) {
  int boro = 0, bigchild = -1;
  for (auto v : adj[u])
    if (sub[v] > boro) boro = sub[v], bigchild = v;
  for (auto v : adj[u])
    if (v != bigchild) sack(v, 0);
  if (bigchild != -1) sack(bigchild, 1);
  for (auto v : adj[u])
    if (v != bigchild)
      for (int i = st[v]; i <= en[v]; i++) mp[lvl[rev[i]]][val[rev[i]]]++;
  mp[lvl[u]][val[u]]++;
  for (auto q : qq[u])
    if (q.first < 100007) ans[q.second] = mp[q.first].size();
  if (!keep)
    for (int i = st[u]; i <= en[u]; i++) {
      mp[lvl[rev[i]]][val[rev[i]]]--;
      if (!mp[lvl[rev[i]]][val[rev[i]]]) mp[lvl[rev[i]]].erase(val[rev[i]]);
    }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> val[i] >> p;
    if (p)
      adj[p].push_back(i);
    else
      mark[i] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (mark[i]) dfs(i, i);
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, k;
    cin >> u >> k;
    qq[u].push_back(pair<int, int>(k + lvl[u], i));
  }
  for (int i = 1; i <= n; i++)
    if (mark[i]) sack(i, 0);
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
