N = int(input())
data = [int(i) for i in input().split()]
Q = int(input())

array = dict()
for i in data:
  if i in array:
    array[i] += 1
  else:
    array[i] = 1
    
for i in range(Q):
  quary = [int(i) for i in input().split()]
  if quary[0] not in array:
    pass
  elif quary[1] in array:
    array[quary[1]] += array[quary[0]]
    del array[quary[0]]
  else:
    array[quary[1]] = array[quary[0]]
    del array[quary[0]]
  
  total = 0
  for i, j in array.items():
    total += i*j
  print(total)