// Arup Guha
// Solution to Jumble Program in Java (for Junior Knights)
// 3/18/2015

import java.util.*;
import java.io.*;

public class jumble {

    public static String[] dictionary;

    public static void main(String[] args) throws Exception {

        // Read in the dictionary.
        Scanner fin = new Scanner(new File("dictionary.in"));
        int n = fin.nextInt();
        dictionary = new String[n];
        for (int i=0; i<n; i++)
            dictionary[i] = fin.next();
        fin.close();

        // Solve each puzzle.
        Scanner stdin = new Scanner(System.in);
        int numPuzzles = stdin.nextInt();
        for (int loop=1; loop<=numPuzzles; loop++) {

            // Case header
            System.out.println("Jumble #"+loop+":");
            String letters = stdin.next();

            // Solve it!
            int[] perm = new int[letters.length()];
            boolean[] used = new boolean[letters.length()];
            solveRec(letters, 0, perm, used);
            System.out.println();
        }
    }

    public static void solveRec(String letters, int k, int[] perm, boolean[] used) {

        // Finished a word.
        if (k == letters.length()) {

            // Create it and see if it's in the dictionary.
            String result = form(letters, perm);
            if (binSearch(result, 0, dictionary.length-1))
                System.out.println("A permutation of "+letters+" that is a valid word is "+result+".");
        }

        // Recursive case.
        else {

            // So we avoid repeats - not required for assignment...
            boolean[] usedLetter = new boolean[26];
            for (int i=0; i<used.length; i++) {

                // First check is for perm, second for repeated letter in slot.
                if (!used[i] && !usedLetter[letters.charAt(i)-'a']) {

                    // Bookkeeping
                    perm[k] = i;
                    used[i] = true;
                    usedLetter[letters.charAt(i)-'a'] = true;

                    // Try this branch.
                    solveRec(letters, k+1, perm, used);
                    used[i] = false;
                }
            }
        }
    }

    // Returns the permutation of letters according to perm.
    public static String form(String letters, int[] perm) {
        String res = "";
        for (int i=0; i<perm.length; i++)
            res = res + letters.charAt(perm[i]);
        return res;
    }

    public static boolean binSearch(String word, int low, int high) {

        // No search space.
        if (low > high) return false;

        // Look in the middle
        int mid = (low+high)/2;
        int result = word.compareTo(dictionary[mid]);

        // Adjust search accordingly.
        if (result < 0)
            return binSearch(word, low, mid-1);
        else if (result > 0)
            return binSearch(word, mid+1, high);

        // If we make it here, we're good!
        return true;
    }
}
