from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())

a = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    a[c] += a[b]
    a[b] = 0
    ans = [x * y for x, y in a.items()]
    print(sum(ans))
