#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   int n, T, A[N], ch[N][26], son[N], fa[N], sz[N], m, tail[N], tg[N], ans[N],       dfn[N], idx, top[N];   int MN[N], out[N];   struct smt {     int t[N << 2], tag[N << 2];     void build(int l, int r, int p) {       if (l == r) {         t[p] = 0x3f3f3f3f;         return;       }       int mid = l + r >> 1;       build(l, mid, p << 1);       build(mid + 1, r, p << 1 | 1);       t[p] = min(t[p << 1], t[p << 1 | 1]);     }     inline void pushdown(int p) {       if (tag[p]) {         tag[p << 1] += tag[p];         tag[p << 1 | 1] += tag[p];         t[p << 1] += tag[p];         t[p << 1 | 1] += tag[p];         tag[p] = 0;       }     }     void modify(int l, int r, int o, const int& pos, const int& val) {       if (l == r)         t[o] = min(t[o], val);       else {         const int mid = l + r >> 1;         pushdown(o);         if (pos <= mid)           modify(l, mid, o << 1, pos, val);         else           modify(mid + 1, r, o << 1 | 1, pos, val);         t[o] = min(t[o << 1], t[o << 1 | 1]);       }     }     void change(int l, int r, int a, int b, int p, int v) {       if (a <= l && r <= b) {         tag[p] += v, t[p] += v;         return;       }       pushdown(p);       int mid = l + r >> 1;       if (a <= mid) change(l, mid, a, b, p << 1, v);       if (b > mid) change(mid + 1, r, a, b, p << 1 | 1, v);       t[p] = min(t[p << 1], t[p << 1 | 1]);     }     int query(int l, int r, int o, const int& L, const int& R) {       if (L <= l && r <= R) return t[o];       pushdown(o);       const int mid = l + r >> 1;       if (L <= mid && mid < R)         return min(query(l, mid, o << 1, L, R),                    query(mid + 1, r, o << 1 | 1, L, R));       if (L <= mid) return query(l, mid, o << 1, L, R);       return query(mid + 1, r, o << 1 | 1, L, R);     }     inline void out(int l, int r, int o) {       if (l == r)         cout << t[o] <<    ;       else {         pushdown(o);         const int mid = l + r >> 1;         out(l, mid, o << 1), out(mid + 1, r, o << 1 | 1);       }     }   } sgt;   void dfs(int now) {     sz[now] = 1;     for (int i = 0; i < 26; ++i)       if (int to = ch[now][i]) {         dfs(to);         sz[now] += sz[to];         if (!son[now] || sz[son[now]] < sz[to]) son[now] = to;       }   }   void dfs2(int now) {     dfn[now] = idx++;     if (son[now]) top[son[now]] = top[now], dfs2(son[now]);     for (int i = 0; i < 26; ++i) {       int to = ch[now][i];       if (to && to != son[now]) dfs2(top[to] = to);     }   }   void add(int x) {     while (top[x] != top[0])       sgt.change(0, n, dfn[top[x]], dfn[x], 1, 1), x = fa[top[x]];     sgt.change(0, n, 0, dfn[x], 1, 1);   }   int query(int x) {     int ret = 0x3f3f3f3f;     while (top[x] != top[0])       ret = min(ret, sgt.query(0, n, 1, dfn[top[x]], dfn[x])), x = fa[top[x]];     ret = min(ret, sgt.query(0, n, 1, 0, dfn[x]));     return ret;   }   void DFS(int now) {     sgt.modify(0, n, 1, dfn[now], MN[now] + !!tg[now]);     for (int i = 0; i < 26; ++i)       if (int to = ch[now][i]) {         MN[to] = min(MN[to], MN[now] + 1);         if (tg[to]) {           add(to);           MN[to] = min(MN[to], query(to));         }         DFS(to);       }   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) {       int pre;       char s[12];       cin >> pre >> s;       fa[i] = pre;       ch[pre][*s -  a ] = i;     }     cin >> m;     for (int i = 1; i <= m; ++i) {       int x;       cin >> x;       tg[x] = i;     }     dfs(0), dfs2(0);     sgt.build(0, n, 1);     memset(MN, 0x3f, sizeof MN);     MN[0] = 0;     DFS(0);     for (int i = 0; i <= n; ++i)       if (tg[i]) out[tg[i]] = MN[i];     for (int i = 1; i <= m; ++i) cout << out[i] <<    ;     return 0;   }