n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = dict()
s = 0
for num in a:
  if num in d:
    d[num] += 1
  else:
    d[num] = 1
  s += num
for i in range(q):
  B, C = list(map(int, input().split()))
  if B in d:
    if C in d:
      d[C] += d[B]
    else:
      d[C] = d[B]
    s += (C-B)*d[B]
    s[B] = 0
  print(s)