import sys

s=sys.stdin.readline

for n in iter(s,'0\n'):

 d={}

 for _ in[0]*int(n):

  k,v=s()[:-1].split()

  d[k]=v

 print(*[d.get(*[s()[0]]*2)for _ in[0]*int(s())],sep='')

import sys

s=sys.stdin.readline

for n in iter(s,'0\n'):

 d={}

 for _ in[0]*int(n):

  e=s()

  d[e[0]]=e[2]

 print((''.join(d.get(*[s()[0]]*2)for _ in[0]*int(s()))))

import sys

s = sys.stdin.readline
for n in iter(s, "0\n"):
    d = {}
    for _ in [0] * int(n):
        k, v = s()[:-1].split()
        d[k] = v
    print(*[d.get(*[s()[0]] * 2) for _ in [0] * int(s())], sep="")

import sys

s = sys.stdin.readline
for n in iter(s, "0\n"):
    d = {}
    for _ in [0] * int(n):
        e = s()
        d[e[0]] = e[2]
    print(("".join(d.get(*[s()[0]] * 2) for _ in [0] * int(s()))))

-        k, v = s()[:-1].split() -        d[k] = v -    print(*[d.get(*[s()[0]] * 2) for _ in [0] * int(s())], sep="") +        e = s() +        d[e[0]] = e[2] +    print(("".join(d.get(*[s()[0]] * 2) for _ in [0] * int(s()))))