401678: GYM100513 C Component Tree

Memory Limit:512 MB Time Limit:6 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Component Treetime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

This problem is a little bit unusual. Here you are to write a program to process queries in online mode. That means that your program will be allowed to read a query only after writing answer for the previous query. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in С++ you've got to use the fflush(stdout) function, in Java — call System.out.flush(), and in Pascal — flush(output).

Imagine a complex application that consists of n components (the application itself is not essential in this problem, so we omit its description). All components form a tree-like hierarchy: each component except one (let's call this special component a main component) is contained in exactly one other component (let's call it a parent component). For the sake of convenience, let's assume that the components are numbered with integers from 1 to n with the main component having the number 1.

Each component has a set of properties associated with it. Each property is given in the form "KEY=VALUE". All property keys within a single component are distinct.

You are given the component tree together with all properties. Your task is to handle several queries for finding the property of a component. Each query is given as a pair (c, key), which means that you have to find the value of the property key for the component number c.

To answer the query, you have to follow this algorithm:

  1. If the value of the property is defined for the component c, report this value as an answer. Otherwise, go to step 2.
  2. If the component number c is not main, continue search in the parent component (set c equal to the number of parent component) and go to the step 1. Otherwise, the value of the property is not defined and considered to be "N/A".
Input

The first line of the input contains an integer n (1 ≤ n ≤ 3·105) — the number of components. Then the input contains the descriptions of components 1, 2, ..., n.

Each description starts with a line containing two space-separated integers pi and ki (0 ≤ pi ≤ n, ki ≥ 0) — the number of parent component and the number of properties. For the first component pi is equal to 0. Each of the following ki lines contains a single property in the form KEY=VALUE, where both KEY and VALUE are strings of Latin letters and digits with length between 1 and 10 characters each. All keys for a given component are distinct. The keys are case-sensitive, so, for example, properties with keys "Length" and "length" are considered distinct. The values are also case-sensitive.

The next line contains an integer q (1 ≤ q ≤ 3·105) — the number of queries you have to answer. Each of the following q lines contains an integer cj (1 ≤ cj ≤ n) and a string keyj, separated by a single space, where keyj is a string of Latin letters and digits with length between 1 and 10 characters. Note that for each query, starting from the second, you will be able to read it only when you print the answer for the previous query and make a flush operation.

It is guaranteed that the total number of properties does not exceed 3·105.

Output

For each query, print a line with the value of the required property. You should make a flush operation after each line.

ExamplesInput
4
0 2
enabled=true
foreground=white
1 2
enabled=false
fontSize=12
2 3
enabled=true
foreground=black
caption=OK
2 1
fontSize=10
5
3 enabled
4 enabled
3 fontSize
4 foreground
2 caption
Output
true
false
12
white
N/A
Input
1
0 2
width=300
height=500
2
1 width
1 Width
Output
300
N/A

加入题单

算法标签: