from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
C = Counter(A)
ans = sum(A)

for _ in range(Q):
    b, c = map(int, input().split())
    ans += c*C[b] - b*C[b]
    C[c] += C[b]
    C[b] = 0
    print(ans)