Hide

Problem A
ChatNOI

Mary is fascinated by the power of large language models. With all the recent hype around chat bots and generative AI, she decided to design her own text generation model called ChatNOI (Chat, but Not Overly Intelligent).

The model is trained on a large document consisting of $n$ words, where it will learn to recognise patterns in sequences of words. Specifically, for every distinct sequence of $k$ consecutive words appearing in the document, the model will keep track of the frequency of words that occur as the next word following this sequence of $k$ words.

As an example, if the model is trained with the parameter $k = 2$ on the document

row row to the fishing rocks
out in the ocean they go
a cow is sitting and rowing
and the sun rises
and the sun sets
but the cow and the boat are still there

it will learn that row row is followed once by the word to, and the is followed twice by the word sun and once by the word boat, the sun is followed once by the word rises and once by the word sets, and so on. We call the frequency of a word following a particular sequence of $k$ words the likelihood of that word following that sequence.

Mary has figured out how she can use a trained model to rank the quality of a given sentence. She looks at every sequence of $k$ consecutive words in the sentence, and the word that follows that sequence. She then calculates the likelihood of that word following that sequence, as per the above definition. The minimum likeliness that she encounters out of all $k$ consecutive words is the quality of that sentence.

Continuing with the above example, the sentence cow and the sun rises has a quality of $1$, because cow and is followed by the with a likelihood of $1$, and the is followed by sun with a likelihood of $2$, and the sun is followed by rises with a likelihood of $1$, the minimum of which is $1$. Similarly the sentence and the sun has a quality of $2$ and the sentence row to the boat has a quality of $0$.

Now that Mary has designed the model and a way to rank the quality of a given sentence, she turns to you for help in using the model to generate sentences. Given the first $k$ words in a sentence and a number $m$, Mary asks you to finish the last $m$ words of that sentence so that it has the maximum quality possible according to the trained model. She is pretty excited so she may even ask you to do this multiple times.

Input

The input consists of:

  • One line with two integers $n$ and $k$, the number of words in the training document and the training parameter $k$ as described above.

  • One line with a sequence of $n$ words $w_1, w_2, \ldots , w_ n$, the training document. Each word consists of $1$ to $10$ lowercase characters from the English alphabet.

  • One line with an integer $q$, the number of queries to follow.

  • $q$ lines, the $i$th of which describes the $i$th query:

    • An integer $m_ i$, where $1 \leq m_ i \leq 5 \cdot 10^5$, the number of words that should be generated to complete the sentence in the $i$th query, and

    • a sequence of $k$ words $u_1, u_2, \ldots , u_ k$, the initial part of the sentence in the $i$th query. Each word is guaranteed to have appeared in the training document.

Let $M$ denote the sum of $m_ i$ over all queries $i$. It is guaranteed that $M$ is at most $5 \cdot 10^5$.

Output

Output $q$ lines, the $i$th line containing the generated words so that the complete sentence for the $i$th query has the maximum quality possible. You may only use words that appear in the training document. If there are multiple possible solutions for a given query then you may output any one of them.

Scoring

Group

Points

Constraints

1

5

$k < n \leq 100$, $k = 1$, $1 \leq q \leq 100$, $m_ i = 1$

2

7

$k < n \leq 5 \cdot 10^5$, $1 \leq k \leq 10$, $1 \leq q \leq 10^5$, $m_ i = 1$

3

17

$k < n \leq 6$, $1 \leq k \leq 10$, $1 \leq q \leq 10$, $1 \leq m_ i \leq 6$

4

18

$k < n \leq 5\, 000$, $1 \leq k \leq 10$, $1 \leq q \leq 5\, 000$, $q \leq M \leq 5\, 000$

5

24

$k < n \leq 5 \cdot 10^5$, $1 \leq k \leq 10$, $1 \leq q \leq 10$, $q \leq M \leq 5 \cdot 10^5$

6

16

$k < n \leq 10^5$, $1 \leq k \leq 10$, $1 \leq q \leq 10^5$, $q \leq M \leq 5 \cdot 10^5$

7

13

$k < n \leq 5 \cdot 10^5$, $1 \leq k \leq 10$, $1 \leq q \leq 10^5$, $q \leq M \leq 5 \cdot 10^5$

Sample Input 1 Sample Output 1
13 2
ullen dullen doff kikke lane koff koffe lane bikke bane ullen dullen doff
3
1 ullen dullen
2 ullen dullen
3 ullen dullen
doff 
doff kikke 
doff kikke lane 
Sample Input 2 Sample Output 2
8 1
buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo
1
7 buffalo
buffalo buffalo buffalo buffalo buffalo buffalo buffalo 
Sample Input 3 Sample Output 3
16 1
have you not heard about the bird the bird bird bird the bird is the word
8
1 have
1 you
1 not
1 heard
1 about
1 the
1 bird
1 is
you 
not 
heard 
about 
the 
bird 
bird 
the 

Please log in to submit a solution to this problem

Log in