from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = defaultdict(int)

for i in a:
  d[i] += 1
s = sum(a)

for i in range(q):
  b, c = map(int, input().split())
  if b < c:
    x = (c-b)*d[b]
    s = s + x
  else:
    x = (b-c)*d[b]
    s = s - x
  d[c] += d[b]
  print (s)