#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  out << "[";
  for (auto k : v) out << k << " ";
  out << "]"
      << "\n";
  return out;
}
const int largo = 1e6 + 100;
bitset<largo> valid;
int res[largo], ord = 1;
int tsize = 1;
struct trie {
  int next['z' - 'a' + 1];
  int id;
  trie() {
    id = 0;
    memset(next, -1, sizeof(next));
  };
  int prox(char c) {
    c -= 'a';
    if (next[c] == -1) next[c] = tsize, tsize++;
    return next[c];
  }
};
trie Trie[2 * largo];
void DFS(int nod, int time, int difer) {
  if (nod == -1) return;
  if (valid[Trie[nod].id]) {
    time = min(time, ord + difer);
    res[Trie[nod].id] = time;
    ord++;
  }
  difer = min(difer, time - ord + 1 + int(valid[nod]));
  for (int c = 0; c < int('z' - 'a' + 1); c++)
    DFS(Trie[nod].next[c], time + 1, difer);
}
int ks[largo];
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < int(n); i++) {
    int p;
    char c;
    cin >> p >> c;
    int prox = Trie[p].prox(c);
    Trie[prox] = trie();
    Trie[prox].id = i + 1;
  }
  int k;
  cin >> k;
  for (int i = 0; i < int(k); i++) {
    cin >> ks[i];
    valid[ks[i]] = true;
  }
  DFS(0, 0, 0);
  for (int i = 0; i < int(k); i++)
    cout << res[ks[i]] << ((i < k - 1) ? " " : "\n");
  return 0;
}
