n=int(input())
A=list(map(int,input().split(" ")))
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split(" "))
  for j in range(n):
    if A[j]==b:
      for k in range(n):
        if A[j+k]!=b:
          break
        else:
          A[j+k]=c
      break
  print(sum(A))