while True:

    n = int(eval(input()))

    if n == 0:

        break

    d = {}

    for _ in range(n):

        a, b = input().split()

        d[a] = b

    m = int(eval(input()))

    ans = ""

    for _ in range(m):

        a = input().strip()

        try:

            ans += d[a]

        except:

            ans += a

    print(ans)
while True:

    n = int(eval(input()))

    if n == 0:

        break

    d = {}

    for _ in range(n):

        a, b = input().split()

        d[a] = b

    m = int(eval(input()))

    ans = ""

    for _ in range(m):

        a = input().strip()

        ans += d[a] if a in d else a

    print(ans)
while True:
    n = int(eval(input()))
    if n == 0:
        break
    d = {}
    for _ in range(n):
        a, b = input().split()
        d[a] = b
    m = int(eval(input()))
    ans = ""
    for _ in range(m):
        a = input().strip()
        try:
            ans += d[a]
        except:
            ans += a
    print(ans)

while True:
    n = int(eval(input()))
    if n == 0:
        break
    d = {}
    for _ in range(n):
        a, b = input().split()
        d[a] = b
    m = int(eval(input()))
    ans = ""
    for _ in range(m):
        a = input().strip()
        ans += d[a] if a in d else a
    print(ans)

-        try: -            ans += d[a] -        except: -            ans += a +        ans += d[a] if a in d else a