import collections as C
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = C.Counter(a)
ans = sum(a)

for _ in range(q):
  b, c = map(int, input().split())
  ans -= d[b]*b
  ans += d[b]*c
  print(ans)
  d[c] += d[b]
  d[b] = 0

  