import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)
 
for i in range(N):
    base += A[i]
    
Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])
 
for i in range(Q-1):
  b ,c = map(int, input().split())
  Ans.append(Ans[i] + (c-b) * cnt[b])
  cnt[c] += cnt[b]
  cnt[b] = 0

print(Ans)