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

counter = Counter(A)
for i in range(Q):
  b, c = map(int, input().split())
  count_b = counter[b]
  counter[b]-= count_b
  counter[c] += count_b
  print(sum([k*v for k, v in counter.items()]))