n = int(input())
la = list(map(int, input().split()))
q = int(input())
sa = set(la)
ls = list(sa)
s = sum(la)
d = dict()
for i in range(len(ls)):
  k = ls[i]
  d[k] = la.count(k)
for j in range(q):
  count = 0
  b, c = map(int, input().split())
  if b in d:
    count = d[b]
    if c in d:
      d[c] += count
    else:
      d[c] = count
  s -= count*(b-c)
  print(s)