#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005, Inf = 1e9;
inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  return sum;
}
int n, m, a[maxn], in[maxn], f[maxn];
vector<pair<int, int> > son[maxn];
int dfs(int u, int w) {
  int cnt = 0;
  if (in[u]) f[u] = min(f[u], w + 1), ++cnt;
  w = min(w, f[u]);
  for (pair<int, int> e : son[u])
    f[e.second] = f[u] + 1, cnt += dfs(e.second, w + cnt);
  return cnt;
}
int main() {
  n = gi();
  char c;
  for (int fa, i = 1; i <= n; ++i) {
    fa = gi();
    scanf("%c", &c);
    son[fa].emplace_back(c - 'a', i);
  }
  for (int i = 0; i <= n; ++i) sort(son[i].begin(), son[i].end());
  m = gi();
  for (int i = 1; i <= m; ++i) in[a[i] = gi()] = 1;
  memset(f + 1, 63, sizeof(f));
  dfs(0, Inf);
  for (int i = 1; i <= m; ++i) printf("%d ", f[a[i]]);
  return 0;
}
