#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int n, k;
int son[N][27], val[N], tim, a[N], f[N];
stack<pair<int, int> > st;
inline void dfs(int cur) {
  if (st.empty() || f[cur] - tim < st.top().second) {
    st.push(make_pair(cur, f[cur] - tim));
  }
  int v;
  tim += val[cur];
  for (int i = 0; i <= 25; i++) {
    if (son[cur][i]) {
      v = son[cur][i];
      f[v] = f[cur] + 1;
      if (!st.empty() && val[v]) f[v] = min(f[v], st.top().second + tim + 1);
      dfs(v);
    }
  }
  if (!st.empty() && st.top().first == cur) st.pop();
}
int main() {
  scanf("%d", &n);
  char x;
  int p;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p);
    cin >> x;
    son[p][x - 'a'] = i;
  }
  scanf("%d", &k);
  memset(val, 0, sizeof(val));
  for (int i = 1; i <= k; i++) scanf("%d", &a[i]), val[a[i]] = 1;
  dfs(0);
  for (int i = 1; i <= k; i++) printf("%d ", f[a[i]]);
  return 0;
}
