Jennifer is practicing for a Crabbles tournament. She pulls a handful of Crabbles tiles out of a bag and tries to form the word with the highest possible score. Each tile shows a letter (used to form the word) and a number (its score value). She may use each tile at most once in her word, and she does not have to use every tile. The word she forms must appear in her dictionary. Her score is the sum of the values of the tiles used in the word.
Note that in Crabbles, different tiles showing the same letter may have different score values.
To check her work, Jennifer would like a program that reports the maximum score possible for a given set of tiles. Your task is to write this program.
The first line contains an integer $N$ ($1 \le N \le 100{,}000$), the number of words in the dictionary. The next $N$ lines each contain one dictionary word, consisting only of lowercase letters. The following line contains an integer $M$ ($1 \le M \le 1{,}000$), the number of Crabbles hands Jennifer wants to play. The $M$ hands follow. Each hand begins with a line containing an integer $P$ ($1 \le P \le 10$), the number of tiles in the hand, followed by $P$ lines, one per tile. Each such line contains a lowercase letter (the letter on the tile), a space, and an integer $V$ ($0 \le V \le 10$), the value of the tile.
For each hand, output a single line containing the maximum score possible with that hand. If no dictionary word can be formed, output $0$.