Under construction

Still being built. Things may change

Kontakt for Two

Time limit2sMemory limit512 MB

Summary
Given a dictionary and queries of (secret word, K), find the maximum number of words the guesser can name before the game ends.
Level

Hard9 of 10

Topics
Trie, Tree, Dynamic programming, String
Solved
No attempts yet

Statement

Elk Valera loves playing Kontakt, a popular word game. Lately he rarely gets to play it, because the game needs at least three players, and gathering three people is hard with limited time and mismatched schedules.

So Valera invented a two-player version called Kontakt for Two. One player picks a non-empty word SS and reveals its first letter. The guesser names words that start with that letter, one after another, and the first player says whether each word is correct. The guesser may not repeat a word.

If the guesser names KK words, where KK is a fixed number, without guessing the word, the first player reveals the second letter. From then on, the guesser may name only words that start with these two letters and that they have not named before. This holds even when the guesser knew only one letter. If, after the second letter appears, the guesser can name another KK words, the first player reveals the third letter, and so on.

If the first player has revealed every letter and the guesser then names KK words starting with the word without naming it, the first player states that the revealed letters form the word, and the game ends. The game also ends when the guesser names the word.

To make the game more interesting, Valera, playing the first player, picks a word that makes the guesser make as many attempts as possible. A correct attempt also counts. The course of the game depends on the guesser's vocabulary and on the chosen KK. Valera knows his friends well, so he can list every word each of them knows. He always picks a word the guesser knows. Choosing the word and KK is not easy.

Valera wants a program that, given a dictionary and QQ pairs of (secret word, KK), finds the maximum number of words the guesser can name before the game ends.

Input

The first line contains a single integer NN, the number of words in the dictionary of the potential guesser. Each of the next NN lines contains one non-empty word of lowercase English letters. The total length of all words does not exceed 2⋅1052 \cdot 10^5.

Words may be identical in spelling. For example, in Russian, замОк and зАмок are different words with the same spelling, because the stress differs. In the language Valera's group plays in, many accent patterns exist, so identically spelled words can be common. During the game, players tell such words apart by how they pronounce them.

The line N+2N+2 contains QQ (1≤Q≤2⋅1051 \leq Q \leq 2 \cdot 10^5), the number of queries. Each of the next QQ lines contains two integers ww and kk (1≤w,k≤N1 \leq w, k \leq N): the index of the secret word in the dictionary and the value of KK. Words are numbered from 1 in input order.

Output

For each query, print on a separate line one integer: the maximum number of words the guesser can name before the game ends by a correct guess or by the first player revealing the word.

Examples3

  1. Example 1

    Input
    6
    asassin
    assistant
    astronaut
    abrakadabra
    abbey
    automaton
    9
    1 1
    1 2
    1 3
    4 1
    4 2
    4 3
    6 1
    6 2
    6 3
    
    Expected output
    3
    5
    6
    3
    4
    5
    2
    3
    4
    
  2. Example 2

    Input
    3
    aa
    ab
    ab
    6
    1 1
    2 1
    1 2
    3 2
    2 2
    3 1
    
    Expected output
    2
    2
    3
    3
    3
    2
    
  3. Example 3

    Input
    7
    pit
    pitbul
    piter
    pitstop
    pitlane
    petroleum
    pistol
    6
    1 2
    1 3
    6 4
    7 2
    7 3
    5 1
    
    Expected output
    6
    7
    5
    5
    7
    4