Auto completion

You are given a set of strings $S$. Each string consists of lowercase Latin letters.

For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions:

• if the current string is $t$, choose some lowercase Latin letter $c$ and append it to the back of $t$, so the current string becomes $t + c$. This action takes $1$ second;
• use autocompletion. When you try to autocomplete the current string $t$, a list of all strings $s \in S$ such that $t$ is a prefix of $s$ is shown to you. This list includes $t$ itself, if $t$ is a string from $S$, and the strings are ordered lexicographically. You can transform $t$ into the $i$-th string from this list in $i$ seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type.

What is the minimum number of seconds that you have to spend to type each string from $S$?

Note that the strings from $S$ are given in an unusual way.

Input

The first line contains one integer $n$ ($1 \le n \le 10^6$).

Then $n$ lines follow, the $i$-th line contains one integer $p_i$ ($0 \le p_i < i$) and one lowercase Latin character $c_i$. These lines form some set of strings such that $S$ is its subset as follows: there are $n + 1$ strings, numbered from $0$ to $n$; the $0$-th string is an empty string, and the $i$-th string ($i \ge 1$) is the result of appending the character $c_i$ to the string $p_i$. It is guaranteed that all these strings are distinct.

The next line contains one integer $k$ ($1 \le k \le n$) — the number of strings in $S$.

The last line contains $k$ integers $a_1$, $a_2$, …, $a_k$ ($1 \le a_i \le n$, all $a_i$ are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set $S$ — formally, if we denote the $i$-th generated string as $s_i$, then $S = {s_{a_1}, s_{a_2}, \dots, s_{a_k}}$.

Output

Print $k$ integers, the $i$-th of them should be equal to the minimum number of seconds required to type the string $s_{a_i}$.

Examples

input

10
0 i
1 q
2 g
0 k
1 e
5 r
4 m
5 h
3 p
3 e
5
8 9 1 10 6


output

2 4 1 3 3


input

8
0 a
1 b
2 a
2 b
4 a
4 b
5 c
6 d
5
2 3 4 7 8


output

1 2 2 4 4


Note

In the first example, $S$ consists of the following strings: ieh, iqgp, i, iqge, ier.

Solution:

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

const int N=1010000;
struct node {
node *s[2],*f;
vector<pair<char,node*> > son;
int val,d,dp;
int mark;
bool isr() { return !f||(f->s[0]!=this && f->s[1]!=this);}
bool dir() { return f->s[1]==this;}
void setc(node *c,int d) { s[d]=c;if (c) c->f=this;}
void seta(int a) {
}
void push() {
rep(i,0,2) if (s[i]) s[i]->seta(add);
}
}
void upd() {
rep(i,0,2) if (s[i]) val=min(val,s[i]->val);

}
}nd[N];
stack<node*> sta;
void rot(node *x) {
node *p=x->f;bool d=x->dir();
if (!p->isr()) p->f->setc(x,p->dir()); else x->f=p->f;
p->setc(x->s[!d],d);x->setc(p,!d);
p->upd();
}
void splay(node *x) {
node *q=x;
while (1) { sta.push(q);if (q->isr()) break; q=q->f; }
while (!sta.empty()) sta.top()->push(),sta.pop();
while (!x->isr()) {
if (x->f->isr()) rot(x);
else if (x->dir()==x->f->dir()) rot(x->f),rot(x);
else rot(x),rot(x);
}
x->upd();
}
node *expose(node *x) {
node *q=NULL;
for (;x;x=x->f) splay(x),x->s[1]=q,(q=x)->upd();
return q;
}

int p[N],n,k,ret[N];
char s[10];

void dfs(node *u) {
expose(u); splay(u);
if (u!=nd) u->d=u->dp=nd[p[u-nd]].dp+1;
u->upd();
if (u->mark) {
//puts("set ff");
u->seta(1); u->push();
if (u->val<u->dp) {
u->dp=u->val;
u->d=min(u->d,u->val+1);
}
u->dp=min(u->dp,u->val);
ret[u->mark]=u->dp;
}
//printf("%d %d %d\n",u-nd,u->dp,u->d);
sort(all(u->son));
for (auto p:u->son) dfs(p.se);
}

int main() {
scanf("%d",&n);
rep(i,1,n+1) {
scanf("%d%s",p+i,s);
nd[p[i]].son.pb(mp(s[0],nd+i));
nd[i].f=nd+p[i];
nd[i].d=nd[i].dp=1<<30; nd[i].upd();

}
scanf("%d",&k);
rep(i,1,k+1) {
int x;
scanf("%d",&x);
nd[x].mark=i;
}
dfs(nd);
rep(i,1,k+1) printf("%d%c",ret[i]," \n"[i==k]);
}