#include <bits/stdc++.h>
using namespace std;
int M[1000001][26];
int pos[1000001];
int ans[1000001];
int tt;
void dfs(int x, int dis, int cnt, int eff_diff) {
  eff_diff = min(eff_diff, dis - tt);
  if (pos[x]) {
    tt++;
    ans[pos[x]] = min(tt + eff_diff, dis);
    dis = ans[pos[x]];
  }
  for (int i = 0; i < 26; i++) {
    int ch = M[x][i];
    if (!ch) continue;
    dfs(ch, dis + 1, cnt, eff_diff);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    char c;
    cin >> c;
    M[p][c - 97] = i;
  }
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    int x;
    cin >> x;
    pos[x] = i + 1;
  }
  dfs(0, 0, 0, 0);
  for (int i = 1; i <= k; i++) cout << ans[i] << " ";
}
