//Задача "Соло на клавиатуре"
//Первая личная интернет-олимпиада сезона 2009-2010
//Автор задачи: Максим Буздалов, buzdalov@rain.ifmo.ru
//Автор решения: Максим Буздалов, buzdalov@rain.ifmo.ru

//Асимптотическая сложность: O(N * M * K * K)
//Планируемое число баллов: 50

import java.io.*;
import java.util.*;
import java.math.*;

public class keyboard_mb_50 {
    private static void myAssert(String msg, boolean exp) {
        if (!exp) {
            throw new AssertionError(msg);
        }
    }

    private static final char[][] neighbour1 = new char[128][];
    private static final char[][][] neighbour2 = new char[128][128][];
    static {
        String[] keyboard = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
        for (int i = 0; i < keyboard.length; ++i) {
            for (int j = 0; j < keyboard[i].length(); ++j) {
                List<Character> ls = new ArrayList<Character>();
                if (j > 0) {
                    ls.add(keyboard[i].charAt(j - 1));
                }
                if (j < keyboard[i].length() - 1) {
                    ls.add(keyboard[i].charAt(j + 1));
                }
                if (i > 0 && j < keyboard[i - 1].length()) {
                    ls.add(keyboard[i - 1].charAt(j));
                }
                if (i > 0 && j + 1 < keyboard[i - 1].length()) {
                    ls.add(keyboard[i - 1].charAt(j + 1));
                }
                if (i + 1 < keyboard.length && j < keyboard[i + 1].length()) {
                    ls.add(keyboard[i + 1].charAt(j));
                }
                if (i + 1 < keyboard.length && j > 0 && j - 1 < keyboard[i + 1].length()) {
                    ls.add(keyboard[i + 1].charAt(j - 1));
                }
                char c = keyboard[i].charAt(j);
                neighbour1[c] = new char[ls.size()];
                for (int k = 0; k < ls.size(); ++k) {
                    neighbour1[c][k] = ls.get(k);
                }
            }
        }
        for (char c1 = 'a'; c1 <= 'z'; ++c1) {
            for (char c2 = 'a'; c2 <= 'z'; ++c2) {
                Set<Character> c = new HashSet<Character>();
                for (char n1 : neighbour1[c1]) {
                    c.add(n1);
                }
                for (char n2 : neighbour1[c2]) {
                    c.add(n2);
                }
                neighbour2[c1][c2] = new char[c.size()];
                Iterator<Character> ic = c.iterator();
                for (int i = 0; i < c.size(); ++i) {
                    neighbour2[c1][c2][i] = ic.next();
                }
            }
        }
    }

    private static void insert(int stringIndex, int what, int[][] results, int[] count) {
        if (stringIndex >= 0) {
            if (results[stringIndex][5] != what) {
                ++count[stringIndex];
                results[stringIndex][5] = what;
                for (int i = 0; i < 5; ++i) {
                    if (results[stringIndex][i] == -1) {
                        results[stringIndex][i] = what;
                        return;
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        File inputFile = new File("keyboard.in");
        myAssert("Input file size is too large", inputFile.length() <= 1048576);
        BufferedReader in = new BufferedReader(new FileReader(inputFile));

        int n = Integer.parseInt(in.readLine());
        myAssert("N is out of bounds", 1 <= n && n <= 50000);
        String[] dict = new String[n];
        for (int i = 0; i < n; ++i) {
            dict[i] = in.readLine();
        }

        int m = Integer.parseInt(in.readLine());
        myAssert("M is out of bounds", 1 <= m && m <= 50000);
        int[][] ans = new int[m][6];
        int[] count = new int[m];
        String[] words = new String[m];
        for (int i = 0; i < m; ++i) {
            String ln = in.readLine();
            words[i] = ln;
            Arrays.fill(ans[i], -1);
        }

        in.close();

        Arrays.sort(dict);

        for (int i = 0; i < n; ++i) {
            String dw = dict[i];
            List<String> fromThis = new ArrayList<String>();
            fromThis.add(dw);
            for (int skipped = 0; skipped < dw.length(); ++skipped) {
                fromThis.add(dw.substring(0, skipped) + dw.substring(skipped + 1));
            }
            for (int pos = 0; pos < dw.length(); ++pos) {
                for (char c : neighbour1[dw.charAt(pos)]) {
                    fromThis.add(dw.substring(0, pos) + c + dw.substring(pos + 1));
                }
            }
            for (char c : neighbour1[dw.charAt(0)]) {
                fromThis.add(c + dw);
            }
            for (int pos = 1; pos < dw.length(); ++pos) {
                for (char c : neighbour2[dw.charAt(pos - 1)][dw.charAt(pos)]) {
                    fromThis.add(dw.substring(0, pos) + c + dw.substring(pos));
                }
            }
            for (char c : neighbour1[dw.charAt(dw.length() - 1)]) {
                fromThis.add(dw + c);
            }
            for (int j = 0; j < m; ++j) {
                for (int k = 0; k < fromThis.size(); ++k) {
                    if (words[j].equals(fromThis.get(k))) {
                        insert(j, i, ans, count);
                    }
                }
            }
        }

        PrintWriter out = new PrintWriter("keyboard.out");
        for (int i = 0; i < m; ++i) {
            out.println(count[i]);
            for (int j = 0; j < 5 && ans[i][j] != -1; ++j) {
                out.println(dict[ans[i][j]]);
            }
        }
        out.close();
    }
}
