#include <bits/stdc++.h>
using namespace std;
const int MX = 1000000 + 9;
class PhawintervalTree {
 private:
  class MaxMinInfo {
   public:
    long long value;
    int index;
    MaxMinInfo(long long val = 0, int idx = -1) : value(val), index(idx) {}
    bool operator<(const MaxMinInfo& that) const {
      if (value != that.value) return value < that.value;
      return index < that.index;
    }
    bool operator>(const MaxMinInfo& that) const {
      if (value != that.value) return value > that.value;
      return index > that.index;
    }
    void addValue(long long val) { value += val; }
  };
  class Node {
   public:
    long long value;
    bool lazyMark;
    long long lazySet;
    long long lazyAdd;
    int lb, ub;
    int lc, rc;
    long long mySum;
    MaxMinInfo myMin, myMax;
    Node(int lower = 0, int upper = 0)
        : value(0),
          lazyMark(false),
          lazySet(0),
          lazyAdd(0),
          lb(lower),
          ub(upper),
          lc(-1),
          rc(-1),
          mySum(0),
          myMin(MaxMinInfo()),
          myMax(MaxMinInfo()) {}
    void setChildren(int leftChild, int rightChild) {
      lc = leftChild;
      rc = rightChild;
    }
  };
  int ROOT;
  vector<Node> node;
  int getNode(Node newNode = Node()) {
    int z = node.size();
    node.push_back(newNode);
    return z;
  }
  int myHalf(int l, int r) { return l + (r - l) / 2; }

 private:
  void reCalculate(int idx) {
    int lc = node[idx].lc;
    int rc = node[idx].rc;
    node[idx].mySum = node[lc].mySum + node[rc].mySum;
    node[idx].myMin = min(node[lc].myMin, node[rc].myMin);
    node[idx].myMax = max(node[lc].myMax, node[rc].myMax);
  }
  int buildNode(int l, int r) {
    int idx = getNode(Node(l, r));
    if (l == r) {
      node[idx].myMin = MaxMinInfo(0, l);
      node[idx].myMax = MaxMinInfo(0, r);
      return idx;
    }
    int h = myHalf(l, r);
    int lc = buildNode(l, h);
    int rc = buildNode(h + 1, r);
    node[idx].setChildren(lc, rc);
    reCalculate(idx);
    return idx;
  }
  void passLazySet(int idx, long long toSet) {
    if (idx == -1) return;
    node[idx].lazyMark = true;
    node[idx].lazySet = toSet;
    node[idx].lazyAdd = 0;
  }
  void passLazyAdd(int idx, long long toAdd) {
    if (idx == -1) return;
    node[idx].lazyAdd += toAdd;
  }
  void resolveLazy(int idx) {
    if (idx == -1) return;
    long long itemCount = node[idx].ub - node[idx].lb + 1;
    if (node[idx].lazyMark) {
      node[idx].lazyMark = false;
      node[idx].mySum = itemCount * node[idx].lazySet;
      node[idx].myMax = MaxMinInfo(node[idx].lazySet, node[idx].ub);
      node[idx].myMin = MaxMinInfo(node[idx].lazySet, node[idx].lb);
      passLazySet(node[idx].lc, node[idx].lazySet);
      passLazySet(node[idx].rc, node[idx].lazySet);
    }
    if (node[idx].lazyAdd != 0) {
      passLazyAdd(node[idx].lc, node[idx].lazyAdd);
      passLazyAdd(node[idx].rc, node[idx].lazyAdd);
      node[idx].mySum += itemCount * node[idx].lazyAdd;
      node[idx].myMax.addValue(node[idx].lazyAdd);
      node[idx].myMin.addValue(node[idx].lazyAdd);
      node[idx].lazyAdd = 0;
    }
    return;
  }
  void addUpdate(int idx, int l, int r, long long val) {
    resolveLazy(idx);
    int lb = node[idx].lb;
    int ub = node[idx].ub;
    if (ub < l || r < lb) return;
    if (l <= lb && ub <= r) {
      passLazyAdd(idx, val);
      resolveLazy(idx);
      return;
    }
    addUpdate(node[idx].lc, l, r, val);
    addUpdate(node[idx].rc, l, r, val);
    reCalculate(idx);
    return;
  }
  void setUpdate(int idx, int l, int r, long long val) {
    resolveLazy(idx);
    int lb = node[idx].lb;
    int ub = node[idx].ub;
    if (ub < l || r < lb) return;
    if (l <= lb && ub <= r) {
      passLazySet(idx, val);
      resolveLazy(idx);
      return;
    }
    setUpdate(node[idx].lc, l, r, val);
    setUpdate(node[idx].rc, l, r, val);
    reCalculate(idx);
    return;
  }
  long long sumHunt(int idx, int l, int r) {
    resolveLazy(idx);
    int lb = node[idx].lb;
    int ub = node[idx].ub;
    if (ub < l || r < lb) return 0;
    if (l <= lb && ub <= r) return node[idx].mySum;
    int lc = node[idx].lc;
    int rc = node[idx].rc;
    return sumHunt(lc, l, r) + sumHunt(rc, l, r);
  }
  MaxMinInfo minHunt(int idx, int l, int r) {
    resolveLazy(idx);
    int lb = node[idx].lb;
    int ub = node[idx].ub;
    if (ub < l || r < lb) return MaxMinInfo(999999999999999999, -1);
    if (l <= lb && ub <= r) return node[idx].myMin;
    int lc = node[idx].lc;
    int rc = node[idx].rc;
    return min(minHunt(lc, l, r), minHunt(rc, l, r));
  }
  MaxMinInfo maxHunt(int idx, int l, int r) {
    resolveLazy(idx);
    int lb = node[idx].lb;
    int ub = node[idx].ub;
    if (ub < l || r < lb) return MaxMinInfo(-999999999999999999, -1);
    if (l <= lb && ub <= r) return node[idx].myMax;
    int lc = node[idx].lc;
    int rc = node[idx].rc;
    return max(maxHunt(lc, l, r), maxHunt(rc, l, r));
  }

 public:
  PhawintervalTree(int n = 1) { ROOT = buildNode(0, n); }
  void rangeAdd(int l, int r, long long val) { addUpdate(ROOT, l, r, val); }
  void rangeSet(int l, int r, long long val) { setUpdate(ROOT, l, r, val); }
  long long getSum(int l, int r) { return sumHunt(ROOT, l, r); }
  long long getMin(int l, int r) { return minHunt(ROOT, l, r).value; }
  int getMinIndex(int l, int r) { return minHunt(ROOT, l, r).index; }
  long long getMax(int l, int r) { return maxHunt(ROOT, l, r).value; }
  int getMaxIndex(int l, int r) { return maxHunt(ROOT, l, r).index; }
};
class PhawintervalArray {
 private:
  int N;
  vector<long long> a;

 public:
  PhawintervalArray(int n = 1) {
    a = vector<long long>(n, 0);
    N = n;
  }
  void rangeAdd(int l, int r, long long val) {
    for (int i = l; i <= r; i++) a[i] += val;
  }
  void rangeSet(int l, int r, long long val) {
    for (int i = l; i <= r; i++) a[i] = val;
  }
  long long getSum(int l, int r) {
    long long ret = 0;
    for (int i = l; i <= r; i++) ret += a[i];
    return ret;
  }
  long long getMin(int l, int r) {
    int cm = l;
    for (int i = l; i <= r; i++)
      if (a[i] < a[cm]) cm = i;
    return a[cm];
  }
  int getMinIndex(int l, int r) {
    int cm = l;
    for (int i = l; i <= r; i++)
      if (a[i] < a[cm]) cm = i;
    return cm;
  }
  long long getMax(int l, int r) {
    int cm = l;
    for (int i = l; i <= r; i++)
      if (a[i] >= a[cm]) cm = i;
    return a[cm];
  }
  int getMaxIndex(int l, int r) {
    int cm = l;
    for (int i = l; i <= r; i++)
      if (a[i] >= a[cm]) cm = i;
    return cm;
  }
  void printStatus(int l, int r, FILE* fpt) {
    fprintf(fpt, "Current Array [%d..%d] : ", l, r);
    for (int i = l; i <= r; i++) fprintf(fpt, "%d ", a[i]);
    fprintf(fpt, "\n");
  }
  void printStatus(FILE* fpt) { printStatus(0, N - 1, fpt); }
  void printStatus() { printStatus(stderr); }
};
int n;
vector<pair<char, int> > edge[MX];
bool isIncluded[MX];
vector<int> questions;
void readInput() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int p;
    char c;
    scanf("%d %c", &p, &c);
    edge[p].push_back(make_pair(c, i));
  }
  for (int i = 0; i <= n; i++) isIncluded[i] = false;
  int k;
  scanf("%d", &k);
  questions = vector<int>(k);
  for (int i = 0; i < k; i++) {
    int t;
    scanf("%d", &t);
    isIncluded[t] = true;
    questions[i] = t;
  }
  return;
}
int lvl[MX];
int sz[MX];
int par[MX];
void dfs1(int node) {
  sz[node] = 0;
  if (isIncluded[node]) sz[node] = 1;
  for (pair<char, int> p : edge[node]) {
    int t = p.second;
    lvl[t] = lvl[node] + 1;
    par[t] = node;
    dfs1(t);
    sz[node] += sz[t];
  }
}
PhawintervalTree tree;
int dp[MX];
void dfs2(int node) {
  int cv = lvl[node];
  if (node == 0) {
    dp[node] = 0;
  } else {
    dp[node] = dp[par[node]] + 1;
    if (isIncluded[node]) {
      int alt = tree.getMin(0, cv - 1);
      dp[node] = min(dp[node], alt);
    }
  }
  tree.rangeSet(cv, cv, dp[node] + 1);
  if (isIncluded[node]) tree.rangeAdd(0, cv, 1);
  for (pair<char, int> p : edge[node]) {
    int t = p.second;
    dfs2(t);
    tree.rangeAdd(0, cv, sz[t]);
  }
  if (isIncluded[node]) tree.rangeAdd(0, cv, -1);
  for (pair<char, int> p : edge[node]) {
    int t = p.second;
    tree.rangeAdd(0, cv, -sz[t]);
  }
}
int main() {
  readInput();
  for (int i = 0; i <= n; i++) sort(edge[i].begin(), edge[i].end());
  tree = PhawintervalTree(n + 1);
  lvl[0] = 0;
  par[0] = -1;
  dfs1(0);
  dfs2(0);
  for (int q : questions) printf("%d ", dp[q]);
  return 0;
}
