"Penguin"
After being inspired by Van Rossum's Python, Kowalski decided to create his own programming language, which he named "Penguin". This language aims to incorporate the best features from other programming languages, making it a strong contender for global popularity.
One notable feature of Penguin is its preprocessor, which functions similarly to the C language preprocessor. This preprocessor processes the program's source code line by line from top to bottom. When it encounters a line starting with "import ", it replaces that line with the contents of the file some_file_name.pen and continues processing the file, including the newly inserted lines.
Let's illustrate how the preprocessor works with an example involving two files: a.pen and b.pen.
Contents of a.pen:
import < b.pen > sayHello("world");
Contents of b.pen:
void sayHello(string name): print("Hello, " + name);
After the preprocessor processes these files, they will appear as follows:
a.pen:
void sayHello(string name): print("Hello, " + name); sayHello("world");
b.pen:
void sayHello(string name): print("Hello, " + name);
The preprocessor operates recursively, continuing to process import directives until all are resolved. This can potentially lead to an infinite loop of file inclusions, in which case the resulting file size is considered infinite.
Kowalski is curious about how the preprocessor affects the size of the files. He has n files written in the Penguin language, some of which include other files. He wants to determine the size of these files after preprocessing. However, since he has been called away to another task, he needs your assistance.
Input
The first line of the input file contains the number n (1 ≤ n ≤ 10000) — the number of files. The following n blocks each contain three lines. The first line of the i–th block contains the number k_i (0 ≤ k_i ≤ 30000) — the number of files included in the i–th file. The second line lists k_i numbers, representing the indices of the files included in file i. All indices are integers from one to n. The third line contains the number s_i (1 ≤ s_i ≤ 10^9) — the number of lines in the i–th file, excluding lines that include other files.
It is guaranteed that the total number of included files does not exceed 30000.
The last line of the input file contains the number p — the number of files for which Kowalski wants to know the size after preprocessing. This is followed by p numbers, indicating the indices of these files.
Output
For each file Kowalski is interested in, print a line with the number of lines it contains after preprocessing. If the number of lines is infinitely large, print -1. It is guaranteed that the size of the output file does not exceed one megabyte.