N = int(input())
A = list(map(int, input().split()))

s = sum(A)
X = [0]*100001
for a in A:
  X[a] += 1

Q = int(input())
for _ in range(Q):
  B, C = map(int, input().split())
  s -= B*X[B]
  s += C*X[B]
  X[C] += X[B]
  X[B] = 0
  print(s)
