#include <bits/stdc++.h>
using namespace std;
void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void one();
struct Node {
  bool isWord;
  map<char, int> children;
  char c;
  int dp;
  int parent;
};
int main() {
  fastio();
  int t = 1;
  for (int i = 0; i < t; ++i) {
    one();
  }
  return 0;
}
vector<int> cnt2;
vector<int> words;
vector<Node> nodes;
int wordsCounter = 0;
int dfs(int u, int autocompleteDist) {
  int wc = 0;
  int parent = nodes[u].parent;
  nodes[u].dp = nodes[parent].dp + 1;
  if (nodes[u].isWord) {
    wc++;
    nodes[u].dp = min(nodes[u].dp, autocompleteDist + 1);
  }
  for (auto &p : nodes[u].children) {
    int v = p.second;
    int wcInSubTree = dfs(v, min(autocompleteDist, nodes[u].dp) + wc);
    wc += wcInSubTree;
  }
  return wc;
}
void one() {
  int n;
  cin >> n;
  Node root;
  root.isWord = false;
  root.dp = -1;
  root.parent = 0;
  nodes.push_back(root);
  for (int i = 1; i <= n; ++i) {
    int pi;
    char c;
    cin >> pi >> c;
    Node node;
    node.isWord = false;
    node.dp = 1e9;
    node.c = c;
    node.parent = pi;
    nodes[pi].children[c] = nodes.size();
    nodes.push_back(node);
  }
  int k;
  cin >> k;
  vector<int> wordIndices;
  for (int i = 0; i < k; ++i) {
    int j;
    cin >> j;
    wordIndices.push_back(j);
    nodes[j].isWord = true;
  }
  dfs(0, 0);
  for (int i = 0; i < k; ++i) {
    cout << nodes[wordIndices[i]].dp << " ";
  }
  cout << endl;
}
