#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 9;
long long par[N], dis[N], mrk[N], what[N], cnt = -1, q[N];
char c[N];
vector<pair<long long, char> > adj[N];
multiset<long long> s;
bool CMP(pair<long long, char> i, pair<long long, char> j) {
  return i.second < j.second;
}
void DFS(long long u) {
  if (u) dis[u] = dis[par[u]] + 1;
  s.insert(what[u] = dis[u] + 1 + mrk[u] - cnt);
  if (mrk[u])
    if (s.size()) dis[u] = min(dis[u], (*s.begin()) + cnt++);
  for (auto v : adj[u]) DFS(v.first);
  s.erase(s.find(what[u]));
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, k;
  cin >> n;
  for (long long i = 1; i <= n; i++)
    cin >> par[i] >> c[i], adj[par[i]].push_back({i, c[i]});
  for (long long i = 0; i <= n; i++) sort(adj[i].begin(), adj[i].end(), CMP);
  cin >> k;
  for (long long i = 0; i < k; i++) cin >> q[i], mrk[q[i]] = 1;
  DFS(0);
  for (long long i = 0; i < k; i++) cout << dis[q[i]] << " ";
  return 0;
}
