#!/usr/bin/env python

# -*- coding: utf-8 -*-



while True:

    dic = {}

    n = int(eval(input()))

    if n == 0:

        break



    for i in range(n):

        key, value = input().split()

        dic[key] = value



    ret = ''



    m = int(eval(input()))

    for i in range(m):

        s = input().split()[0]

        ret += dic.get(s, s)



    print(ret)
#!/usr/bin/env python

# -*- coding: utf-8 -*-



while True:

    dic = {}

    n = int(eval(input()))

    if n == 0:

        break



    for i in range(n):

        key, value = input().split()

        dic[key] = value



    ret = ''



    m = int(eval(input()))

    for i in range(m):

        s = input().strip()

        ret += dic.get(s, s)



    print(ret)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
while True:
    dic = {}
    n = int(eval(input()))
    if n == 0:
        break
    for i in range(n):
        key, value = input().split()
        dic[key] = value
    ret = ""
    m = int(eval(input()))
    for i in range(m):
        s = input().split()[0]
        ret += dic.get(s, s)
    print(ret)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
while True:
    dic = {}
    n = int(eval(input()))
    if n == 0:
        break
    for i in range(n):
        key, value = input().split()
        dic[key] = value
    ret = ""
    m = int(eval(input()))
    for i in range(m):
        s = input().strip()
        ret += dic.get(s, s)
    print(ret)

-        s = input().split()[0] +        s = input().strip()