n = int(input())
a = list(map(str, input().split()))
q = int(input())
b = [0]*q
c = [0]*q

for i in range(q):
    p = list(map(str, input().split()))
    b[i] = p[0]
    c[i] = p[1]

for i in range(q):
    cnt = 0
    for h in range(n):
        if a[h] == b[i]:
            a[h] = c[i]
        cnt += int(a[h])
    print(cnt)
