n = int(input())
As = list(map(int, input().split()))
q = int(input())
Ds = [0] * (10 ** 5 + 1)
for a in As:
  Ds[a] += 1
for i in range(q):
  b, c = map(int, input().split())
  Ds[c] += Ds[b]
  Ds[b] = 0
  ans = 0
  for x, i in enumerate(Ds):
    ans += x * i
  print(ans)