#include <bits/stdc++.h>
using namespace std;
const int nsz = 1e6;
const int inf = 0x3f3f3f3f;
bool on[nsz + 5];
int n, m, rt = 1, pr[nsz + 5], dfn[nsz + 5], dp[nsz + 5], id[nsz + 5],
          ans[nsz + 5];
vector<pair<char, int> > g[nsz + 5];
void solve(int u = rt, int mn = inf) {
  static int t;
  dfn[u] = on[u] ? ++t : t;
  dp[u] = dp[pr[u]] + 1;
  if (on[u]) dp[u] = min(dp[u], mn + dfn[u]);
  if (u == rt) dp[u] = 0;
  mn = min(mn, dp[u] - dfn[u] + on[u]);
  if (id[u]) ans[id[u]] = dp[u];
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i].second;
    solve(v, mn);
  }
}
int main() {
  scanf("%d", &n);
  n += 1;
  for (int u = 2; u <= n; ++u) {
    char c;
    scanf("%d %c\n", &pr[u], &c);
    pr[u] += 1;
    g[pr[u]].push_back(make_pair(c, u));
  }
  for (int u = 1; u <= n; ++u) sort(g[u].begin(), g[u].end());
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int u;
    scanf("%d", &u);
    u += 1;
    on[u] = 1, id[u] = i;
  }
  solve();
  for (int i = 1; i <= m; ++i) {
    printf("%d ", ans[i]);
  }
}
