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