N=int(input())
A=list(input().split())
Q=int(input())
anlist=[]
BC=[input().split() for i in range(Q)]
for i in range(Q):
  if BC[i][0] in A:
    while True:
      A[A.index(BC[i][0])]=BC[i][1]
      if BC[i][0] not in A:
        break
  A=list(map(int,A))
  print(sum(A))
  A=list(map(str,A))
  
