First of Her Name
In the Royal Family, names are very important! As the Royal Historian you have been charged with analyzing the patterns in the names of the Royal Ladies in the realm.
There have been Royal Ladies, for convenience numbered from to . The name of each Lady is an uppercase letter concatenated with the name of her mother. The exception is the Lady numbered , the founder of the Royal Family, whose name is just a single uppercase letter.
For example, ENERYS could be the mother of AENERYS (as the name AENERYS consists of the single uppercase letter ‘A’ concatenated with ENERYS, which is her mother’s name). Similarly, AENERYS could be the mother of DAENERYS and YAENERYS.
You are given the description of all the Royal Ladies. Your task is to determine, for certain interesting strings , the number of Royal Ladies for whom is a prefix of their name. For example, consider Sample Input 1 below, with a Royal Line that goes straight from the founder S to AENERYS (through YS, RYS, ERYS, NERYS and ENERYS), with each Lady having exactly one daughter. Then AENERYS has two daughters — DAENERYS and YAENERYS, with the latter having one daughter, RYAENERYS.
In such a family, RY is a prefix of the names of two ladies: RYS and RYAENERYS. E is a prefix of the names of ERYS and ENERYS. N is a prefix only of NERYS’s name, while S is a prefix only of the name of the founder, S. AY is not a prefix of any Royal Lady’s name.
Input
The first line contains two integers and , where is the total number of Royal Ladies and is the number of query strings.
Then follow lines describing the Royal Ladies. The -th of these lines describes the Royal Lady numbered and contains an uppercase letter and an integer , where is the first letter of the name of Lady , and and for is the number of her mother (or , in the case of the First Lady). All the names are unique.
The remaining lines each contain one nonempty query string, consisting only of uppercase letters. The sum of the lengths of the query strings is at most .
Output
Output lines, with the -th line containing the number of Royal Ladies who have the -th query string as a prefix of their name.