Entangled Tree

Given a forest of split nodes, order the leaf labels so each split node's leaves are consecutive, choosing the lexicographically smallest sequence, then answer position queries.

Medium7TreeDFSSortingGreedyNo attempts yetTime limit8sMemory limit512 MB

Problem

Each development department at Ishimatsu Electronics owns one product family, such as disks and storage, network equipment, or mobile phones. A department covers so many kinds of product that the staff in the product management division had trouble deciding which category a new product belongs to. One of them proposed a drawing called a category diagram.

A category diagram is drawn on one large sheet of paper. The names of the development departments go on the upper side of the sheet. These are the start nodes. Questions that tell product features apart go in the middle. These are the split nodes. The category names go on the lower side. These are the end nodes. Each start node is joined to a single split node or a single end node, and every line going down from a split node is labeled with an answer to that question. To classify a product you begin at the start node of the department that built it, follow the lines downward, and read the name written on the end node you reach.

The drawing is easy to read, but the hand drawn diagrams were messy because lines crossed many times. The staff asked for a clean diagram instead, one in which no two lines cross.

Ignore the text of the questions and use integer labels from 11 to NN in place of the category names. The y-coordinate and the label list of each split node fix every connection in the diagram. Now place the end nodes one per position, from position 11 on the left through position NN on the right. A placement has no crossing lines exactly when, for every split node, the end nodes hanging below it occupy consecutive positions. Several placements can satisfy this, so take the one whose label sequence, read from position 11 through position NN, comes first in lexicographic order.

A query gives one position number. Answer with the label of the end node placed there.

Input

The input consists of several datasets. Each dataset has the following format.

N M Q
split node info 1
split node info 2
...
split node info M
query 1
query 2
...
query Q

The first line holds the number of end nodes NN, the number of split nodes MM, and the number of queries QQ. The next MM lines describe one split node each, in the following format.

Y L label1 label2 ...

YY is the y-coordinate of that split node, and a smaller value is higher up. LL is the length of the label list, followed by LL end node labels. The labels within one list are distinct.

The label lists fix the connections. For an end node label ee, let C(e)C(e) be the split nodes whose list contains ee, ordered by increasing y-coordinate.

  • Two neighbors in C(e)C(e) are connected. The one with the smaller y-coordinate is above, the other is below.
  • The last node of C(e)C(e), the one with the largest y-coordinate, is connected to end node ee.
  • If C(e)C(e) is empty, end node ee is connected straight to a start node.
  • A split node connected to no split node above it is connected to a start node.

A split node is connected to at most one split node above it, so the whole diagram is a forest whose leaves are the end nodes.

The split node lines are followed by QQ lines holding one query each. A query is a horizontal position of an end node, and the leftmost position is numbered 11.

The constraints are as follows.

  • 1N1000001 \le N \le 100000
  • 0MN10 \le M \le N - 1
  • 1Q10001 \le Q \le 1000, QNQ \le N
  • 0Y1090 \le Y \le 10^9, and the y-coordinates within one dataset are distinct
  • 1L1 \le L, and every label is between 11 and NN
  • the sum of LL within one dataset is at most 200000200000
  • every query is between 11 and NN
  • there are at most 100100 datasets, the sum of NN over the whole input is at most 200000200000, and the sum of LL over the whole input is at most 500000500000

The dataset with N=M=Q=0N = M = Q = 0 marks the end of the input and is not processed.

Output

For each dataset print QQ lines. Line ii holds the label of the end node placed at the position given by query ii. Use the placement that has no crossing lines and whose label sequence comes first in lexicographic order.

Print one blank line after the output of each dataset.