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

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

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

public class keyboard_mb_80_1 {
    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 class Node {
        private int endsThere = -1;
        private Node[] next;

        public void insert(String s, int index, int wordLabel) {
            if (index == s.length()) {
                endsThere = wordLabel;
            } else {
                if (next == null) {
                    next = new Node[26];
                }
                int idx = s.charAt(index) - 'a';
                if (next[idx] == null) {
                    next[idx] = new Node();
                }
                next[idx].insert(s, index + 1, wordLabel);
            }
        }
    }

    private static int go0(Node n, String s, int index, int skipped) {
        if (index == skipped) {
            ++index;
        }
        if (s.length() == index) {
            return n.endsThere;
        } else {
            int ch = s.charAt(index) - 'a';
            if (n.next == null || n.next[ch] == null) {
                return -1;
            } else {
                return go0(n.next[ch], s, index + 1, skipped);
            }
        }
    }

    private static int go1(Node n, String s, int index, int changed, char forWhat) {
        if (s.length() == index) {
            return n.endsThere;
        } else {
            int ch = ((index == changed) ? forWhat : s.charAt(index)) - 'a';
            if (n.next == null || n.next[ch] == null) {
                return -1;
            } else {
                return go1(n.next[ch], s, index + 1, changed, forWhat);
            }
        }
    }

    private static int go2(Node n, String s, int index, int inserted, char what) {
        if (index == inserted) {
            int ch = what - 'a';
            if (n.next == null || n.next[ch] == null) {
                return -1;
            } else {
                return go2(n.next[ch], s, index, -1, '!');
            }
        } else if (index == s.length()) {
            return n.endsThere;
        } else {
            int ch = s.charAt(index) - 'a';
            if (n.next == null || n.next[ch] == null) {
                return -1;
            } else {
                return go2(n.next[ch], s, index + 1, inserted, what);
            }
        }
    }

    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);
        Node wordRoot = new Node();
        int[][] ans = new int[m][6];
        int[] count = new int[m];
        int[] ri = new int[m];
        Map<String, Integer> rehash = new HashMap<String, Integer>();
        for (int i = 0; i < m; ++i) {
            String ln = in.readLine();
            Integer rh = rehash.get(ln);
            if (rh == null) {
                rehash.put(ln, rh = i);
                wordRoot.insert(ln, 0, i);
            }
            ri[i] = rh;
            Arrays.fill(ans[i], -1);
        }

        in.close();

        long t0 = System.currentTimeMillis();
        Arrays.sort(dict);
        System.out.println(System.currentTimeMillis() - t0);

        for (int i = 0; i < n; ++i) {
            String dw = dict[i];
            for (int skipped = -1; skipped < dw.length(); ++skipped) {
                insert(go0(wordRoot, dw, 0, skipped), i, ans, count);
            }
            for (int pos = 0; pos < dw.length(); ++pos) {
                for (char c : neighbour1[dw.charAt(pos)]) {
                    insert(go1(wordRoot, dw, 0, pos, c), i, ans, count);
                }
            }
            for (char c : neighbour1[dw.charAt(0)]) {
                insert(go2(wordRoot, dw, 0, 0, c), i, ans, count);
            }
            for (int pos = 1; pos < dw.length(); ++pos) {
                for (char c : neighbour2[dw.charAt(pos - 1)][dw.charAt(pos)]) {
                    insert(go2(wordRoot, dw, 0, pos, c), i, ans, count);
                }
            }
            for (char c : neighbour1[dw.charAt(dw.length() - 1)]) {
                insert(go2(wordRoot, dw, 0, dw.length(), c), i, ans, count);
            }
        }

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