import sys

input = sys.stdin.readline
n = int(input())
a = list(map(str, input().strip().split()))
q = int(input())
bc = [map(str, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
ii = 0

for i in b:
    a = [n.replace(i,c[ii]) for n in a]
    print(sum(list(map(int, a))))
    ii += 1