P3879 [TJOI2010] Reading Comprehension

Description

The English teacher assigned $N$ reading-comprehension passages. Each passage contains many unfamiliar words that would require checking a dictionary. To save time, we want to collect statistics to determine in which passages certain words appear.

Input Format

The first line contains an integer $N$, the number of passages. Each passage contains only spaces and lowercase letters. The next $N$ lines each describe one passage. Each line begins with an integer $L$, the number of words in that passage. Then follow $L$ words, separated by a single space. Then an integer $M$ follows, the number of queries. After that, there are $M$ lines, each containing one word to query.

Output Format

For each query word, output one line listing the indices of the passages in which it appears, in ascending order, without duplicates. Indices are separated by a single space (note that there should be no space before the first index or after the last index). If the word never appears, output an empty line.

Explanation/Hint

For $30\%$ of the testdata, $1\le M\le 10^3$. For $100\%$ of the testdata, $1\le M\le 10^4$, $1\le N\le 10^3$. Each passage length (including spaces between adjacent words) $\le 5\times 10^3$ characters, and each word length $\le 20$ characters. Thanks to @钟梓俊 for adding a set of testdata. Translated by ChatGPT 5