from collections import Counter
n = int(input())
a = list(map(int, input().split()))
ans = sum(a)
s = Counter(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    ans += s[b]*(c-b)
    print(ans)
    s[c] += s[b]
    s[b] = 0