x = [0] * 100_001
value = 0

n = int(input())
for a in map(int, input().split()):
  x[a] += 1
  value += a
q = int(input())
for _ in range(q):
  b, c = map(int, input().split())
  value += (c - b) * x[b]
  x[c] += x[b]
  x[b] = 0
  print(value)