from collections import Counter

n = int(input())
a = Counter(map(int, input().split()))
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  a[c] += a[b]
  del a[b]
  s = sum(x * y for x, y in a.items())
  print(s)
