from collections import Counter

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