while True:

    n = int(eval(input()))

    if n == 0:break

    c = {}

    for _ in range(n):

        a,b = input().split()

        c[a] = b

    m = int(eval(input()))

    ans = ''

    for _ in range(m):

        a = input()[0]

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

    print(ans)
import sys

cin = sys.stdin

while True:

    n = int(cin.readline())

    if n == 0:break

    c = {}

    for _ in range(n):

        a,b = cin.readline().strip().split()

        c[a] = b

    m = int(cin.readline())

    ans = ''

    for _ in range(m):

        a = cin.readline().strip()

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

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

import sys

cin = sys.stdin
while True:
    n = int(cin.readline())
    if n == 0:
        break
    c = {}
    for _ in range(n):
        a, b = cin.readline().strip().split()
        c[a] = b
    m = int(cin.readline())
    ans = ""
    for _ in range(m):
        a = cin.readline().strip()
        ans += c[a] if a in c else a
    print(ans)

+import sys + +cin = sys.stdin -    n = int(eval(input())) +    n = int(cin.readline()) -        a, b = input().split() +        a, b = cin.readline().strip().split() -    m = int(eval(input())) +    m = int(cin.readline()) -        a = input()[0] +        a = cin.readline().strip()