#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e6 + 555;
struct node {
  node *nex[26];
  int id, sum, ans;
  node() {
    for (int i = 0; i < 26; i++) nex[i] = nullptr;
    sum = 0;
    id = -1;
  }
} st[maxn];
node *append(node *fa, char ch) { return fa->nex[ch - 'a'] = new node(); }
int ans[maxn];
void dfs(node *root, int dep) {
  root->ans = dep;
  for (int i = 0; i < 26; i++) {
    if (root->nex[i] != nullptr) {
      dfs(root->nex[i], dep + 1);
      root->sum += root->nex[i]->sum;
    }
  }
}
void dfs2(node *root, int inq) {
  if (root->id != -1) root->ans = min(root->ans, inq);
  inq = min(root->ans + 1, inq) + (root->id != -1);
  for (int i = 0; i < 26; i++) {
    if (root->nex[i] != nullptr) {
      root->nex[i]->ans = root->ans + 1;
      dfs2(root->nex[i], inq);
      inq += root->nex[i]->sum;
    }
  }
}
void dfs3(node *root) {
  if (root->id != -1) {
    ans[root->id] = root->ans;
  }
  for (int i = 0; i < 26; i++) {
    if (root->nex[i] != nullptr) {
      dfs3(root->nex[i]);
    }
  }
}
node *root[maxn];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n;
  root[0] = new node();
  for (int i = 1; i <= n; i++) {
    static int a;
    static string b;
    cin >> a >> b;
    root[i] = append(root[a], b.front());
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    static int idx;
    cin >> idx;
    root[idx]->id = i;
    root[idx]->sum = 1;
  }
  dfs(root[0], 0);
  dfs2(root[0], numeric_limits<int>::max());
  dfs3(root[0]);
  for (int i = 1; i <= m; i++) cout << ans[i] << ' ';
  cout << endl;
  return 0;
}
