Under construction

Still being built. Things may change

Keyboard Solo

Time limit2sMemory limit1024 MB

Summary
Given a typed word with at most one keyboard mistake, list the dictionary words that could have produced it, in lexicographic order, up to five.
Level

Medium6 of 10

Topics
Hash map, String, String matching
Solved
No attempts yet

Problem

Staff at the Ministry of Fast Printing and Long Strings (MBPDS) are worried about a new problem. Recently the texts they type have become harder, and they make more mistakes. Fortunately, MBPDS staff are well trained, so each worker makes at most one mistake in any word.

The possible mistakes are:

  • a neighboring key is pressed instead of the needed key;
  • the needed key is not pressed;
  • one of the neighboring keys is pressed before the needed key;
  • one of the neighboring keys is pressed after the needed key.

Six keys count as neighbors of a key: left, right, upper left, upper right, lower left, and lower right. The keyboard is a standard QWERTY keyboard, shown in the figure.

The Ministry wants to automate error correction. Write a program that, given a word with at most one mistake, prints the dictionary words that could have been typed.

Input

The first line contains NN (1≤N≤5⋅1041 \le N \le 5 \cdot 10^4), the number of words in the dictionary. Each of the next NN lines contains one dictionary word. All dictionary words are distinct.

The next line contains MM (1≤M≤5⋅1041 \le M \le 5 \cdot 10^4), the number of words to check. Each of the next MM lines contains one word to check.

All words consist of uppercase Latin letters. The input file has no empty lines and is at most one megabyte.

Output

For each word to correct, print a block of one or more lines. The first line of the block is CiC_i, the number of dictionary words that could match the typed word. Then print those dictionary words in lexicographic order, one per line. If Ci>5C_i > 5, print only the first five words.

Examples2

  1. Example 1

    Input
    7
    solving
    contests
    is
    what
    makes
    us
    happy
    3
    solvung
    js
    qwerty
    
    Expected output
    1
    solving
    2
    is
    us
    0
    
  2. Example 2

    Input
    6
    letterq
    letterw
    lettere
    letterr
    lettert
    lettery
    2
    letter
    lertterq
    
    Expected output
    6
    lettere
    letterq
    letterr
    lettert
    letterw
    1
    letterq