#include <bits/stdc++.h>
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 1e6 + 10;
int n, m, delta, q[N];
struct trie {
  int ch[26], f;
  bool fin;
} t[N];
std::stack<std::pair<int, int> > s;
inline void dfs(int x) {
  if (s.empty() || t[x].f - delta < s.top().second)
    s.push(std::make_pair(x, t[x].f - delta));
  delta += t[x].fin;
  for (int i = 0; i < 26; i++) {
    if (!t[x].ch[i]) continue;
    t[t[x].ch[i]].f = t[x].f + 1;
    if (!s.empty() && t[t[x].ch[i]].fin)
      t[t[x].ch[i]].f = std::min(t[t[x].ch[i]].f, s.top().second + delta + 1);
    dfs(t[x].ch[i]);
  }
  if (!s.empty() && s.top().first == x) s.pop();
}
char str[2];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    scanf("%s", str + 1);
    t[x].ch[str[1] - 'a'] = i;
  }
  m = read();
  for (int i = 1; i <= m; i++) t[q[i] = read()].fin = true;
  dfs(0);
  for (int i = 1; i <= m; i++) printf("%d ", t[q[i]].f);
  return 0;
}
