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

//Решение mb_100 с перепутанным порядком применения ошибок.

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

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

    private static final int HASH_MUL = 31;
    private static final int HASH_INV = -1108378657;
    private static final int[] HASH_POW = new int[1100000];
    static {
        HASH_POW[0] = 1;
        for (int i = 1; i < HASH_POW.length; ++i) {
            HASH_POW[i] = HASH_MUL * HASH_POW[i - 1];
        }
    }

    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 interface Filter {
        public int hashCode(ModifiableString s);
        public char charAt(ModifiableString s, int index);
        public int length(ModifiableString s);
    }

    private static final Filter IDENTITY = new Filter() {
        public int hashCode(ModifiableString s) {
            return s.prefixHash[s.prefixHash.length - 1];
        }
        public char charAt(ModifiableString s, int index) {
            return s.data[index];
        }
        public int length(ModifiableString s) {
            return s.data.length;
        }
    };

    private static final Filter REPLACE = new Filter() {
        public int hashCode(ModifiableString s) {
            int old = s.prefixHash[s.prefixHash.length - 1];
            return old + (s.replacement - s.data[s.index]) * HASH_POW[s.index];
        }
        public char charAt(ModifiableString s, int index) {
            return index == s.index ? s.replacement : s.data[index];
        }
        public int length(ModifiableString s) {
            return s.data.length;
        }
    };

    private static final Filter SKIP = new Filter() {
        public int hashCode(ModifiableString s) {
            int prefix = s.prefixHash[s.index];
            int suffix = (s.prefixHash[s.prefixHash.length - 1] - s.prefixHash[s.index + 1]) * HASH_INV;
            return prefix + suffix;
        }
        public char charAt(ModifiableString s, int index) {
            return index < s.index ? s.data[index] : s.data[index + 1];
        }
        public int length(ModifiableString s) {
            return s.data.length - 1;
        }
    };

    private static final Filter INSERT = new Filter() {
        public int hashCode(ModifiableString s) {
            int prefix = s.prefixHash[s.index];
            int suffix = (s.prefixHash[s.prefixHash.length - 1] - s.prefixHash[s.index]) * HASH_MUL;
            int infix = HASH_POW[s.index] * s.replacement;
            return prefix + suffix + infix;
        }
        public char charAt(ModifiableString s, int index) {
            if (index == s.index) {
                return s.replacement;
            } else if (index < s.index) {
                return s.data[index];
            } else {
                return s.data[index - 1];
            }
        }
        public int length(ModifiableString s) {
            return s.data.length + 1;
        }
    };

    private static class ModifiableString {
        final char[] data;
        final int[] prefixHash;
        int index;
        char replacement;

        int order;

        Filter filter = IDENTITY;

        public ModifiableString(char[] data) {
            myAssert("data is empty", data.length > 0);
            for (char c : data) {
                myAssert("data contains an illegal character", 'a' <= c && c <= 'z');
            }
            this.data = data;
            this.prefixHash = new int[data.length + 1];
            int h = 0;
            prefixHash[0] = h;
            for (int i = 0; i < data.length; ++i) {
                h += data[i] * HASH_POW[i];
                prefixHash[i + 1] = h;
            }
        }

        public void setModification(int index, char replacement, Filter filter) {
            this.filter = filter;
            this.index = index;
            this.replacement = replacement;
        }

        public int length() {
            return filter.length(this);
        }

        public char charAt(int index) {
            return filter.charAt(this, index);
        }

        public int hashCode() {
            return filter.hashCode(this);
        }

        public boolean equals(Object o) {
            if (o instanceof ModifiableString) {
                ModifiableString ms = (ModifiableString) o;
                int l = length();
                if (l != ms.length()) {
                    return false;
                }
                for (int i = 0; i < l; ++i) {
                    if (charAt(i) != ms.charAt(i)) {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
    }

    private static class Top5 {
        private ModifiableString[] top = new ModifiableString[5];
        private int size = 0;

        public int size() {
            return size;
        }
        public ModifiableString get(int index) {
            return top[index];
        }
        public void add(ModifiableString s) {
            for (int index = size; ; --index) {
                if (index == 0 || top[index - 1].order < s.order) {
                    for (int t = size; t > index; --t) {
                        if (t < top.length) {
                            top[t] = top[t - 1];
                        }
                    }
                    if (index < top.length) {
                        top[index] = s;
                    }
                    if (size < 5) {
                        ++size;
                    }
                    return;
                }
            }
        }
        public void clear() {
            Arrays.fill(top, null);
            size = 0;
        }
    }

    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());
        ModifiableString[] dictionary = new ModifiableString[n];
        for (int i = 0; i < n; ++i) {
            dictionary[i] = new ModifiableString(in.readLine().toCharArray());
        }
        int m = Integer.parseInt(in.readLine());
        ModifiableString[] words = new ModifiableString[m];
        for (int i = 0; i < m; ++i) {
            words[i] = new ModifiableString(in.readLine().toCharArray());
        }
        in.close();

        Arrays.sort(dictionary, new Comparator<ModifiableString>() {
            public int compare(ModifiableString lhs, ModifiableString rhs) {
                return lhs.length() - rhs.length();
            }
        });
        int[] next = new int[n];
        int[] bucket = new int[26];
        ModifiableString[] tmp = new ModifiableString[n];

        for (int index = dictionary[n - 1].length() - 1; index >= 0; --index) {
            int count = n - 1;
            while (count >= 0) {
                if (dictionary[count].length() <= index) {
                    break;
                }
                --count;
            }
            Arrays.fill(bucket, -1);
            Arrays.fill(next, count + 1, n, -1);
            for (int i = n - 1; i > count; --i) {
                int ci = dictionary[i].charAt(index) - 'a';
                next[i] = bucket[ci];
                bucket[ci] = i;
            }
            for (int bi = 0, sz = 0; bi < 26; ++bi) {
                for (int x = bucket[bi]; x != -1; x = next[x]) {
                    tmp[sz++] = dictionary[x];
                }
            }
            for (int i = count + 1, j =0; i < n; ++i, ++j) {
                dictionary[i] = tmp[j];
            }
        }

        for (int i = 0; i < n; ++i) {
            dictionary[i].order = i;
        }

        Map<ModifiableString, ModifiableString> dictMap = new HashMap<ModifiableString, ModifiableString>();
        for (ModifiableString s : dictionary) {
            dictMap.put(s, s);
        }

        PrintWriter out = new PrintWriter("keyboard.out");
        Top5 result = new Top5();
        for (ModifiableString s : words) {
            int sl = s.length();

            result.clear();
            ModifiableString t = dictMap.get(s);
            if (t != null) {
                result.add(t);
            }
            for (int i = sl - 1; i >= 0; --i) {
                s.setModification(i, 'a', SKIP);
                t = dictMap.get(s);
                if (t != null) {
                    result.add(t);
                }
            }
            s.setModification(0, 'a', IDENTITY);
            for (int i = sl - 1; i >= 0; --i) {
                char old = s.charAt(i);
                for (char n1 : neighbour1[old]) {
                    s.setModification(i, n1, REPLACE);
                    t = dictMap.get(s);
                    if (t != null) {
                        result.add(t);
                    }
                }
            }
            s.setModification(0, 'a', IDENTITY);
            for (int i = sl; i >= 0; --i) {
                char[] n2;
                if (i == 0) {
                    n2 = neighbour1[s.charAt(0)];
                } else if (i == sl) {
                    n2 = neighbour1[s.charAt(sl - 1)];
                } else {
                    n2 = neighbour2[s.charAt(i - 1)][s.charAt(i)];
                }
                for (char c : n2) {
                    s.setModification(i, c, INSERT);
                    t = dictMap.get(s);
                    if (t != null) {
                        result.add(t);
                    }
                }
                s.setModification(0, 'a', IDENTITY);
            }
            out.println(result.size());
            for (int i = 0; i < result.size(); ++i) {
                out.println(result.get(i).data);
            }
        }
        out.close();
    }
}
