ChatNOI

아직 제출이 없습니다시간 제한4초메모리 제한1024 MB

문제

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 nn words, where it will learn to recognise patterns in sequences of words. Specifically, for every distinct sequence of kk 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 kk words.

As an example, if the model is trained with the parameter k=2k = 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 kk 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 kk 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 kk consecutive words is the quality of that sentence.

Continuing with the above example, the sentence cow and the sun rises has a quality of 11, because cow and is followed by the with a likelihood of 11, and the is followed by sun with a likelihood of 22, and the sun is followed by rises with a likelihood of 11, the minimum of which is 11. Similarly the sentence and the sun has a quality of 22 and the sentence row to the boat has a quality of 00.

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 kk words in a sentence and a number mm, Mary asks you to finish the last mm 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.

입력

The input consists of:

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

  • One line with a sequence of nn words w_1,w_2,,w_nw\_1, w\_2, \ldots, w\_n, the training document. Each word consists of 11 to 1010 lowercase characters from the English alphabet.

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

  • qq lines, the iith of which describes the iith query:

    • An integer m_im\_i, where 1m_i51051 \leq m\_i \leq 5 \cdot 10^5, the number of words that should be generated to complete the sentence in the iith query, and
    • a sequence of kk words u_1,u_2,,u_ku\_1, u\_2, \ldots, u\_k, the initial part of the sentence in the iith query. Each word is guaranteed to have appeared in the training document.

Let MM denote the sum of m_im\_i over all queries ii. It is guaranteed that MM is at most 51055 \cdot 10^5.

출력

Output qq lines, the iith line containing the generated words so that the complete sentence for the iith 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.