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)]
i = 0

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