n = int(input())
A = list(map(int, input().split()))
K = [0]*(10**5+1)
s = 0
for a in A:
  K[a] += 1
  s += a
q = int(input())
for _ in range(q):
  b, c = map(int, input().split())
  t = K[b]
  K[b] = 0
  K[c] += t
  s += t*(c-b)
  print(s)