func_code_string: #include <bits/stdc++.h>   using namespace std;   inline int readint() {     int a;     cin >> a;     return a;   }   int toInt(string &s) {     const static int Mod = 998244353;     const static long long bas = 29;     int res = 0;     for (int i = 0, n = s.length(); i < n; ++i)       res = (res * bas + s[i] -  a  + 1) % Mod;     return res;   }   const int MaxN = 100005;   vector<int> v[MaxN];   map<int, int> *dp[MaxN];   int siz[MaxN], son[MaxN];   void build(int x) {     siz[x] = 1, son[x] = x;     for (auto y : v[x]) {       dp[y] = dp[x] + 1, build(y);       if (siz[y] >= siz[son[x]]) son[x] = y;       siz[x] += siz[y];     }   }   int val[MaxN];   void calc(int x, bool f) {     if (f)       dp[x][0][val[x]]++;     else {       dp[x][0][val[x]]--;       if (dp[x][0][val[x]] == 0) dp[x][0].erase(val[x]);     }     for (auto y : v[x]) calc(y, f);   }   vector<pair<int, int> > query[MaxN];   int ans[MaxN];   void dfs(int x, bool f) {     for (auto y : v[x])       if (y != son[x]) dfs(y, false);     if (son[x] != x) dfs(son[x], true);     for (auto y : v[x])       if (y != son[x]) calc(y, true);     dp[x][0][val[x]]++;     for (auto t : query[x]) {       int dep = t.first;       int id = t.second;       ans[id] = dp[x][dep].size();     }     if (not f) calc(x, false);   }   map<int, int> cnt[MaxN];   bool root[MaxN];   int main() {     int n = readint();     string s;     int fa;     for (int i = 1; i <= n; ++i) {       cin >> s >> fa;       if (!fa)         root[i] = true;       else         v[fa].push_back(i);       val[i] = toInt(s);     }     for (int i = 1; i <= n; ++i)       if (root[i]) dp[i] = cnt, build(i);     int T = readint();     pair<int, int> p;     for (int i = 1, a, b; i <= T; ++i) {       a = readint();       b = readint();       if (dp[a] + b >= cnt + MaxN) continue;       p.first = b;       p.second = i;       query[a].push_back(p);     }     for (int i = 1; i <= n; ++i)       if (root[i]) dfs(i, false);     for (int i = 1; i <= T; ++i) printf( %d n , ans[i]);     return 0;   }   