import string



n = int(input())

while n != 0:

	f = []

	t = []

	for i in range(n):

		l = input().split()

		f.append(l[0])

		t.append(l[1])

	d = string.maketrans(''.join(f), ''.join(t))

	s = []

	for i in range(int(input())):

		s.append(input().strip())

	print(''.join(s).translate(d))

	n = int(input())
import string



n = int(input())

while n != 0:

	l  = [input().split() for i in range(n)]

	f = [i for i,j in l]

	t = [j for i,j in l]

	d = string.maketrans(''.join(f), ''.join(t))

	s = [input().strip() for i in range(int(input()))]

	print(''.join(s).translate(d))

	n = int(input())
import string

n = int(input())
while n != 0:
    f = []
    t = []
    for i in range(n):
        l = input().split()
        f.append(l[0])
        t.append(l[1])
    d = string.maketrans("".join(f), "".join(t))
    s = []
    for i in range(int(input())):
        s.append(input().strip())
    print("".join(s).translate(d))
    n = int(input())

import string

n = int(input())
while n != 0:
    l = [input().split() for i in range(n)]
    f = [i for i, j in l]
    t = [j for i, j in l]
    d = string.maketrans("".join(f), "".join(t))
    s = [input().strip() for i in range(int(input()))]
    print("".join(s).translate(d))
    n = int(input())

-    f = [] -    t = [] -    for i in range(n): -        l = input().split() -        f.append(l[0]) -        t.append(l[1]) +    l = [input().split() for i in range(n)] +    f = [i for i, j in l] +    t = [j for i, j in l] -    s = [] -    for i in range(int(input())): -        s.append(input().strip()) +    s = [input().strip() for i in range(int(input()))]