from collections import Counter

N = int(input())
A = [int(x) for x in input().split()]
d = Counter(A)
Q = int(input())

def sum_from_counter(d):
  return sum(map(lambda k: k * d[k], d))

for i in range(Q):
  B, C = [int(x) for x in input().split()]
  d[C] = d.get(C, 0) + d[B]
  del d[B]
  print(sum_from_counter(d))