from collections import Counter
N = int(input())
A = list(map(int, input().split()))
ans = sum(A)
cnt = Counter(A)
Q = int(input())
for _ in range(Q):
    a, b = map(int, input().split())
    ans += (b-a)*cnt[a]
    cnt[b] += cnt[a]
    cnt[a] = 0
    print(ans)