#include <bits/stdc++.h>
using namespace std;
int n;
const int LEN = 1e6;
struct TREE {
  long long seg[4 * LEN], lazy[4 * LEN];
  void pro(int n, int s, int e) {
    seg[n] = min(seg[n], lazy[n]);
    if (s != e) {
      lazy[n + n] = min(lazy[n], lazy[n + n]);
      lazy[n + n + 1] = min(lazy[n], lazy[n + n + 1]);
    }
    lazy[n] = 1e9;
  }
  void update_range(int l, int r, long long val, int n = 1, int s = 1,
                    int e = ::n) {
    pro(n, s, e);
    if (s > r || e < l || l > r) return;
    if (s >= l && e <= r) {
      lazy[n] = min(lazy[n], val);
      pro(n, s, e);
      return;
    }
    update_range(l, r, val, n + n, s, (s + e) / 2);
    update_range(l, r, val, n + n + 1, (s + e) / 2 + 1, e);
    seg[n] = min(seg[n + n], seg[n + n + 1]);
  }
  int get(int l, int r, int n = 1, int s = 1, int e = ::n) {
    pro(n, s, e);
    if (s > r || e < l || l > r) return 1e9;
    if (s >= l && e <= r) return seg[n];
    int a = get(l, r, n + n, s, (s + e) / 2);
    int b = get(l, r, n + n + 1, (s + e) / 2 + 1, e);
    return min(a, b);
  }
};
TREE A, B;
const int nax = 1e6 + 10;
vector<pair<int, int>> g[nax];
int val[nax], sz[nax];
int fr[nax], to[nax], T, depth[nax];
void dfs(int u) {
  sz[u] = val[u];
  fr[u] = ++T;
  for (auto [w, v] : g[u]) {
    depth[v] = depth[u] + 1;
    dfs(v);
    sz[u] += sz[v];
  }
  to[u] = T;
}
int ans[nax], order[nax];
void calc(int u, int rem) {
  int best = A.get(fr[u], fr[u]) + depth[u];
  if (!u) best = 0;
  if (val[u] == 1) {
    best = min(best, B.get(fr[u], fr[u]) + order[u]);
  }
  ans[u] = min(order[u], best);
  A.update_range(fr[u], to[u], best - depth[u]);
  B.update_range(fr[u], to[u], best - rem);
  for (auto [w, v] : g[u]) {
    calc(v, rem + val[u]);
    rem += sz[v];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  for (int i = 0; i < 4 * LEN; i++)
    A.seg[i] = A.lazy[i] = B.seg[i] = B.lazy[i] = 1e9;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    char c;
    cin >> a >> c;
    g[a].push_back({c - 'a', i});
  }
  for (int i = 0; i <= n; i++) {
    sort((g[i]).begin(), (g[i]).end());
  }
  n++;
  int m;
  cin >> m;
  vector<pair<int, int>> q;
  for (int i = 1; i <= m; i++) {
    int s;
    cin >> s;
    q.push_back({s, i});
    val[s] = 1;
  }
  dfs(0);
  sort((q).begin(), (q).end(), [&](pair<int, int> a, pair<int, int> b) {
    return fr[a.first] < fr[b.first];
  });
  int cnt = 0;
  for (auto [a, b] : q) {
    order[a] = ++cnt;
  }
  calc(0, 0);
  vector<int> res(m + 1);
  for (auto [a, b] : q) {
    res[b] = ans[a];
  }
  for (int i = 1; i <= m; i++) cout << res[i] << ' ';
  cout << '\n';
}
