#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using ld = long double;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <class T>
T sqr(T x) {
  return x * x;
}
const ld pi = 4 * atan(1.);
const int inf = 1e9 + 7;
const int N = 1e6 + 17;
int n, m, k;
vector<pair<char, int>> g[N];
int t[4 * N], how[4 * N];
int ok[N], ans[N], in[N], b[N];
int timer = 0;
void push(int v) {
  if (how[v] == 0) return;
  how[2 * v + 1] += how[v];
  how[2 * v + 2] += how[v];
  t[2 * v + 1] += how[v];
  t[2 * v + 2] += how[v];
  how[v] = 0;
}
void upd(int v, int tl, int tr, int pos, int x) {
  if (tl == tr) {
    t[v] = x;
    return;
  }
  push(v);
  int c = (tl + tr) >> 1;
  if (pos > c)
    upd(2 * v + 2, c + 1, tr, pos, x);
  else
    upd(2 * v + 1, tl, c, pos, x);
  t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
void modify(int v, int tl, int tr, int l, int r, int x) {
  if (tl > r || l > tr) return;
  if (tl <= l && r <= tr) {
    how[v] += x;
    t[v] += x;
    return;
  }
  push(v);
  int c = (tl + tr) >> 1;
  modify(2 * v + 1, tl, c, l, r, x);
  modify(2 * v + 2, c + 1, tr, l, r, x);
  t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
void dfs(int v, int pr = -1) {
  in[v] = timer++;
  if (pr == -1)
    ans[v] = 0;
  else
    ans[v] = ans[pr] + 1;
  if (ok[v]) {
    int cur = t[0] + 1;
    ans[v] = min(ans[v], cur);
  }
  upd(0, 0, n - 1, in[v], ans[v]);
  if (ok[v]) modify(0, 0, n - 1, 0, in[v], 1);
  for (auto x : g[v]) dfs(x.second, v);
  upd(0, 0, n - 1, in[v], inf);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    char c;
    cin >> x >> c;
    g[x].push_back({c, i + 1});
  }
  n++;
  for (int i = 0; i < n; i++) sort((g[i]).begin(), (g[i]).end());
  for (int i = 0; i < 4 * N; i++) t[i] = inf;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    ok[b[i]] = 1;
  }
  dfs(0);
  for (int i = 0; i < m; i++) cout << ans[b[i]] << " ";
}
