#include <bits/stdc++.h>
using namespace std;
const int N = 1000043;
map<char, int> nxt[N];
bool term[N];
int n, k;
char buf[3];
int dp[N];
int dict[N];
int T[4 * N];
int f[4 * N];
void build(int v, int l, int r) {
  T[v] = int(1e9);
  if (l != r - 1) {
    int m = (l + r) / 2;
    build(v * 2 + 1, l, m);
    build(v * 2 + 2, m, r);
  }
}
int getVal(int v) { return T[v] + f[v]; }
void push(int v, int l, int r) {
  T[v] += f[v];
  if (l != r - 1) {
    f[v * 2 + 1] += f[v];
    f[v * 2 + 2] += f[v];
  }
  f[v] = 0;
}
void upd(int v, int l, int r) {
  if (l != r - 1) {
    T[v] = min(getVal(v * 2 + 1), getVal(v * 2 + 2));
  }
}
int get(int v, int l, int r, int L, int R) {
  if (L >= R) return int(1e9);
  if (l == L && r == R) return getVal(v);
  push(v, l, r);
  int m = (l + r) / 2;
  int ans = min(get(v * 2 + 1, l, m, L, min(m, R)),
                get(v * 2 + 2, m, r, max(L, m), R));
  upd(v, l, r);
  return ans;
}
void add(int v, int l, int r, int L, int R, int val) {
  if (L >= R) return;
  if (l == L && r == R) {
    f[v] += val;
    return;
  }
  push(v, l, r);
  int m = (l + r) / 2;
  add(v * 2 + 1, l, m, L, min(m, R), val);
  add(v * 2 + 2, m, r, max(L, m), R, val);
  upd(v, l, r);
}
void setVal(int v, int l, int r, int pos, int val) {
  if (l == r - 1) {
    f[v] = 0;
    T[v] = val;
    return;
  }
  push(v, l, r);
  int m = (l + r) / 2;
  if (pos < m)
    setVal(v * 2 + 1, l, m, pos, val);
  else
    setVal(v * 2 + 2, m, r, pos, val);
  upd(v, l, r);
}
void dfs(int v, int d, int last) {
  dp[v] = last + 1;
  if (term[v]) dp[v] = min(dp[v], get(0, 0, n + 1, 0, d));
  setVal(0, 0, n + 1, d, dp[v] + 1);
  if (term[v]) add(0, 0, n + 1, 0, d + 1, 1);
  for (auto x : nxt[v]) dfs(x.second, d + 1, dp[v]);
  setVal(0, 0, n + 1, d, int(1e9));
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int p;
    scanf("%d %s", &p, buf);
    char c = buf[0];
    nxt[p][c] = i + 1;
  }
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    scanf("%d", &dict[i]);
    term[dict[i]] = true;
  }
  build(0, 0, n + 1);
  dfs(0, 0, -1);
  for (int i = 0; i < k; i++) printf("%d ", dp[dict[i]]);
  puts("");
}
