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

S = sum(A)
counter = Counter(A)
for i in range(Q):
    B,C = list(map(int,input().split()))
    S += counter[B]*C - counter[B]*B
    counter[C] += counter[B]
    print(S)
