408460: GYM103134 E Learning new languages

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Learning new languagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Baledoc recently joined college and is preparing to become the best programmer in the world. At one of the college's events, he made a website using #React, #GraphQL, #Typescript, #Jest, #Golang, #Postgres, #Kafka, #Blockchain, #Machine #Learning, #Docker and #Kubernetes. At the end of the project, he didn't really know what the site was doing, but he was amazed by all the new things he learned and needs to learn #more.

He noticed that the path to being the best programmer in the world starts with learning all languages, so he decided to go to the bookstore and buy all the dictionaries he could find.

Upon arriving home, he noticed that dictionaries are not the best way to learn a new language, but he will not #give #up. Conveniently, given his perseverance, he already understands a few words from each of the languages ​​in the dictionaries he bought.

Every word in the dictionary is defined in terms of other words. Baledoc can understand a new word when it can be defined in terms of words that he already understands, for that, he needs to understand all the words that define it. Help Baledoc find out how many words he can understand in total.

#Submit2Learn #Learn1000000007Submit

Input

The first line of the entry contains an integer $$$ n $$$ ($$$ 1 \leq n \leq 1000 $$$) ─ the number of words that Baledoc already knows. The second line contains the $$$ n $$$ words $$$ k_i $$$ $$$ (1 \leq | k_i | \leq 10) $$$ known by Baledoc, separated by space.

The third line of the entry contains an integer $$$ m $$$ ($$$ 0 \leq m \leq 100 $$$) ─ the number of words in the dictionary. The next $$$ 2m $$$ lines contains information about the dictionary, $$$ 2 $$$ lines for each word.

For each word, the first line contains a string $$$ s $$$ ($$$ 1 \leq | s | \leq 10 $$$) and an integer $$$ k $$$ ($$$ 1 \leq k \leq 10 $$$) ─ the word that is defined in the dictionary and the number of words used in its definition, respectively. The second line contains the $$$ k $$$ words $$$ s_i $$$ ($$$ 1 \leq | s_i | \leq 10 $$$) used to define $$$ s $$$, separated by space.

All words in the entry have only lower case letters from $$$ a $$$ to $$$ z $$$.

Output

A single integer, the amount of words Baledoc can understand in total.

ExamplesInput
9
you one more of a or cat have than
3
two 4
one more than one
cats 6
two or more of a cat
i 1
you
Output
12
Input
3
a b d
3
abacaba 3
aba c aba
abadaba 3
aba d aba
aba 3
a b a
Output
5
Note

For the first case, Baledoc initially understands 9 words ("you", "one", "more", "of", "a", "or", "cat", "have", "than") and succeeds understand all the words in the dictionary. He understands "two" and "i" because he understands all the necessary words, and now that he understands "two", he can understand "cats", which uses "two" in his definition.

加入题单

算法标签: