After talking to Python Van Rossum, Kowalski decided to invent his own programming language and call it "Penguin". The main feature of this language is that it will take the best of all other programming languages, which will undoubtedly make it the most popular language in the world.
In addition, this language will have a preprocessor very similar to the preprocessor of the C language. The preprocessor processes the source code of the program line by line from top to bottom. If the current line is of the form "import ", it replaces this line with the source code of the file some_file_name.pen and continues processing the file (including the newly inserted lines).
Let's consider the operation of the preprocessor using an example. Suppose there are two files — a.pen and b.pen.
Source code of a.pen:
import < b.pen > sayHello("world");
Source code of b.pen:
void sayHello(string name): print("Hello, " + name);
After the preprocessor, the files will look as follows:
a.pen:
void sayHello(string name): print("Hello, " + name); sayHello("world");
b.pen:
void sayHello(string name): print("Hello, " + name);
Note that the preprocessor works recursively: it keeps processing inclusion directives until all of them are handled. It is easy to see that this may result in an infinite process of file inclusion. In such a case, the resulting file size is considered infinite.
However, after running the preprocessor, the program's source code may increase significantly. Kowalski has files in Penguin language, some of which include other files. He is interested in the size of the files after the preprocessor is applied. Since he has been called to a new task, he asks you to help him.
The first line of the input file contains (1 \leq n \leq 10000) — the number of files. The following blocks each contain three lines. In the first line of the –th block is k_i (0 \leq k_i \leq 30000) — the number of files included in the –th program. In the second line of the –th block, there are k_i numbers — the indices of the files included in the –th file. All indices are integers from one to . In the third line of the –th block is s_i (1 \leq s_i \leq 10^9) — the number of lines in the –th file, not considering lines for including other files.
The total number of included files does not exceed .
The last line of the input file contains — the number of files for which Kowalski wants to know the size after the preprocessor. Then in this line there are numbers — the indices of these files.
Write lines to the output file — for each file Kowalski is interested in, the number of lines it has after the preprocessor. If this number is infinitely large, write -1. The output file size is guaranteed not to exceed one megabyte.
Example 1