n = int(input())
seq = list(map(int,input().split()))
count = {}
total = sum(seq)
for item in seq:
  count[item] = seq.count(item)

q = int(input())

for i in range(q):
  b,c = map(int,input().split())
  tmp = count[b]
  total += tmp*(c-b)
  count[c] = tmp
  count[b] = 0
  print(total)
  