n = int(input())
a = list(map(int, input().split()))
q = int(input())

l = {}
ans = []
for e in a:
    l[e] = l[e] + 1 if l.get(e) else 1

sum = sum(a)

for _ in range(q):
    b, c = map(int, input().split())
    if b in l:
        sum += (c - b) * l[b]
        l[c] = l[c] + l[b] if l.get(c) else l[b]
    ans.append(sum)

for sum in ans:
    print(sum)