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

for _ in range(Q):
  B, C = map(int, input().split())
  count = A.count(B)
  if count >0:
    A.remove(B)
    A.extend([C]*count)
  print(sum(A))