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

base = sum(A)

Q = int(input())

for i in range(Q):
  B, C = map(int, input().split())
  # Aに現れるBの個数xがわかればよい
  x = A.count(B)
  base += x*(C-B)
  print(base)
  A = [a if a != B else C for a in A] 

  