#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') w = -w;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * w;
}
template <class T>
inline T read(T& x) {
  return x = read<T>();
}
const int N = 1e6 + 10, inf = 1e9;
int val[4 * N], tag[4 * N];
void build(int x, int l, int r) {
  val[x] = inf, tag[x] = 0;
  if (l == r) return;
  build((x << 1), l, ((l + r) >> 1)),
      build((x << 1 | 1), ((l + r) >> 1) + 1, r);
}
inline void push_up(int x) { val[x] = min(val[(x << 1)], val[(x << 1 | 1)]); }
inline void push_down(int x) {
  if (tag[x]) {
    val[(x << 1)] += tag[x], tag[(x << 1)] += tag[x];
    val[(x << 1 | 1)] += tag[x], tag[(x << 1 | 1)] += tag[x];
    tag[x] = 0;
  }
}
void insert(int x, int l, int r, int p, int v) {
  if (l == r) {
    val[x] = v, tag[x] = 0;
    return;
  }
  push_down(x);
  if (p <= ((l + r) >> 1))
    insert((x << 1), l, ((l + r) >> 1), p, v);
  else
    insert((x << 1 | 1), ((l + r) >> 1) + 1, r, p, v);
  push_up(x);
}
void modify(int x, int l, int r, int ql, int qr, int v) {
  if (ql <= l and r <= qr) {
    val[x] += v, tag[x] += v;
    return;
  }
  push_down(x);
  if (ql <= ((l + r) >> 1)) modify((x << 1), l, ((l + r) >> 1), ql, qr, v);
  if (qr > ((l + r) >> 1))
    modify((x << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v);
  push_up(x);
}
int query(int x, int l, int r, int ql, int qr) {
  if (ql <= l and r <= qr) return val[x];
  push_down(x);
  if (qr <= ((l + r) >> 1)) return query((x << 1), l, ((l + r) >> 1), ql, qr);
  if (ql > ((l + r) >> 1))
    return query((x << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr);
  return min(query((x << 1), l, ((l + r) >> 1), ql, qr),
             query((x << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr));
}
int n, ch[N][26];
int term[N], dict[N], dp[N];
void dfs(int x, int d, int last) {
  dp[x] = last + 1;
  if (term[x]) dp[x] = min(dp[x], query(1, 0, n, 0, d - 1));
  insert(1, 0, n, d, dp[x] + 1);
  if (term[x]) modify(1, 0, n, 0, d, 1);
  for (int c = 0; c < 26; ++c)
    if (ch[x][c]) dfs(ch[x][c], d + 1, dp[x]);
  insert(1, 0, n, d, inf);
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    int x = read<int>();
    char c[2];
    scanf("%s", c);
    ch[x][c[0] - 'a'] = i;
  }
  int m = read<int>();
  for (int i = 1; i <= m; ++i) term[read(dict[i])] = 1;
  build(1, 0, n);
  dfs(0, 0, -1);
  for (int i = 1; i <= m; ++i) printf("%d%c", dp[dict[i]], " \n"[i == m]);
  return 0;
}
