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

for i in range(Q):
  BC = list(map(int, input().split()))
  B_i = BC[0]
  C_i = BC[1]
  for j in range(len(A)):
    if A[j] == B_i:
      A[j] = C_i
  A = sorted(A)
  print(sum(A))
  