from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [list(map(int, input().split())) for _ in range(Q)]

A_counter = Counter(A)
for b in B:
    cnt = A_counter[b[0]]
    A_counter[b[1]] += cnt
    del A_counter[b[0]]
    print(sum([num * cnt for num, cnt in A_counter.items()]))
