#include <bits/stdc++.h>
const int mod = 998244353;
const int inf = 1000000009;
const long long INF = 1000000000000000009;
const long long big = 1000000000000000;
const long double eps = 0.000000000000000000001;
using namespace std;
int T[1000005][26];
int S[1000005], D[1000005], A[1000005];
int numer = 0;
void append(int n, int x) {
  numer++;
  T[n][x] = numer;
}
void licz(int n) {
  if (S[n]) D[n]++;
  for (int i = 0; i < 26; i++) {
    if (T[n][i]) {
      licz(T[n][i]);
      D[n] += D[T[n][i]];
    }
  }
}
void dfs(int n, int d, int c) {
  c = min(c, d);
  if (S[n]) {
    A[S[n]] = min(c + 1, d);
    d = min(c + 1, d);
  }
  int ile = 0;
  if (S[n]) ile++;
  for (int i = 0; i < 26; i++) {
    if (T[n][i]) {
      dfs(T[n][i], d + 1, c + ile);
      ile += D[T[n][i]];
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    char c;
    cin >> a >> c;
    append(a, c - 'a');
  }
  int k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    int a;
    cin >> a;
    S[a] = i;
  }
  licz(0);
  dfs(0, 0, 0);
  for (int i = 1; i <= k; i++) cout << A[i] << " ";
  return 0;
}
