#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll MOD = 998244353LL;
template <class T>
void chmin(T& a, T b) {
  a = min(a, b);
}
template <class T>
void chmax(T& a, T b) {
  a = max(a, b);
}
struct Node {
  char c;
  int ord, dp;
  bool f;
  Node* to[26];
  Node(char cc) {
    c = cc;
    f = false;
    for (int i = 0; i < 26; i++) {
      to[i] = nullptr;
    }
  }
};
struct Trie {
  Node* rt;
  int pos;
  Trie() {
    rt = new Node('1');
    pos = 0;
  }
  Node* insert(Node* nd, char c) {
    Node* now = nd->to[(int)(c - 'a')];
    if (now == nullptr) {
      now = new Node(c);
      nd->to[(int)(c - 'a')] = now;
    }
    return now;
  }
  void etour(Node* now) {
    if (now->f) pos++;
    now->ord = pos;
    for (int i = 0; i < 26; i++) {
      if (now->to[i] != nullptr) {
        etour(now->to[i]);
      }
    }
  }
  void dfs(Node* now, int pd, int mi) {
    if (now->f) {
      now->dp = min(pd + 1, mi + now->ord);
      chmin(mi, now->dp - now->ord + 1);
    } else {
      now->dp = pd + 1;
      chmin(mi, now->dp - now->ord);
    }
    for (int i = 0; i < 26; i++) {
      if (now->to[i] != nullptr) {
        dfs(now->to[i], now->dp, mi);
      }
    }
  }
};
Node* ns[1000000];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  Trie tr;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    --p;
    char c;
    cin >> c;
    if (p == -1) {
      ns[i] = tr.insert(tr.rt, c);
    } else {
      ns[i] = tr.insert(ns[p], c);
    }
  }
  int q;
  cin >> q;
  vector<int> a(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i];
    --a[i];
    ns[a[i]]->f = true;
  }
  tr.etour(tr.rt);
  tr.dfs(tr.rt, -1, n + 100000);
  for (int i = 0; i < q; i++) {
    cout << ns[a[i]]->dp;
    if (i + 1 < q) {
      cout << " ";
    } else {
      cout << '\n';
    }
  }
}
