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

counter = Counter(A)
sum_res = sum(A)

for _ in range(Q):
  B,C = map(int,input().split())
  sum_res = sum_res - B*counter[B] + C*counter[C]
  counter[C] += counter[B]
  counter[B] = 0
  
  print(sum_res)