import numpy as np

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

for i in range(Q):
  B,C = map(int,input().split())
  for j in range(N):
    A = np.where(A==B,C,A)
  print(np.sum(A))