n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

m = 10**5
d = set(a)
s = sum(a)
l = [0]*m
for i in d:
    l[i-1] = a.count(i)
for b, c in bc:
    s = s + l[b-1]*(c-b)
    l[c-1] += l[b-1]
    l[b-1] = 0
    print(s)