#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define TRUE 1
#define FALSE 0

/*#define DBG*/

#ifdef DBG
#define DEBUG if (TRUE)
#else
#define DEBUG if (FALSE)
#endif

#define CL(a, b) memset(a, b, sizeof(a));
#define CL2d(a,b,x,y) memset(a, b, sizeof(a[0][0])*x*y)
#define MAX(X, Y) (((X)>(Y) ? (X) : (Y)))
#define MIN(X, Y) (((X)<(Y) ? (X) : (Y)))

/* GLOBALS */
char dictionary[91][5]; /* I'm guessing each morse code is at most 4 symbols? */
char words[100][101];
char morsewords[100][401];
int nw;
/* END GLOBALS */

void dump()
{

}

int get_input()
{
    int i, j;
    char tmp;
    /* get letter representations */
    for (i = 0; i < 26; ++i)
    {
        scanf("%c ", &tmp);
        scanf("%4s ", dictionary[(int)tmp]);
    }

    scanf("%d ", &nw);
    for (i = 0; i < nw; ++i)
        scanf("%100s ", words[i]);

    for (i = 0; i < nw; ++i)
        for (j = 0; j < strlen(words[i]); ++j)
            strncat(morsewords[i], dictionary[(int)words[i][j]], 4);


    return TRUE;
}

void process()
{
    int ns, i, j, found;
    char line[410], line2[410];
    char ANSWER[100][101];
    CL2d(ANSWER, 0, 100, 101);

    while (1) {
        /* ACTUAL PROCESSING! */
        scanf("%d ", &ns);
        DEBUG { printf("ns: %d\n", ns); }
        if (ns == 0)
            break;
        for (j = 0; j < ns; ++j)
        {
            found = 0;
            fgets(line, 401, stdin);
            line[strlen(line)-1] = 0; /* get rid of the new line at the end */
            for (i = 0; i < nw; ++i)
            {
                if (strcmp(morsewords[i], line) == 0)
                {
                    /* FOUND A VALID WORD! */
                    found = 1;
                    strncpy(ANSWER[j], words[i], 100);
                    break;
                }
            }
            DEBUG { printf("found: %d j: %d\n", found, j); }
            if (!found)
            {
                for (;j < ns-1; ++j)
                    fgets(line2, 401, stdin);
                DEBUG { printf("found: %d j: %d\n", found, j); }
                break;
            }
        }
        if (!found)
            printf("%s not in dictionary.\n", line); /*OMG!*/
        else
        {
            printf("%s", ANSWER[0]);
            for (j = 1; j < ns; ++j)
                printf(" %s", ANSWER[j]);
            puts("");
        }

        CL2d(ANSWER, 0, 100, 101);
    }
}

int main(int args, char** argv)
{

    /*while (get_input())*/
    {
        get_input();
        process();

        /* CLEAR GLOBALS! */
        CL2d(dictionary, 0, 91, 5);
        CL2d(words, 0, 100, 100);
        CL2d(morsewords, 0, 100, 400);
        /* GLOBALS CLEARED! */
    }

    return 0;
}