from collections import defaultdict as dd
d = dd(int)
N = int(input())
for i in [int(n) for n in input().split()]:
  d[i] = d[i]+1
times = int(input())
for i in range(times):
  a, b = [int(n) for n in input().split()]
  d[b], d[a] = d[b] + d[a], 0
  print(sum([ key*value for key, value in d.items() if value > 0]))
  