import numpy

tmp=input()
list=input().split(" ")
#list = map((lambda e: int(e)), list)
#array=numpy.zeros(len(list),dtype=">i4")
#for i in range(len(list)):
#  array[i]=int(list[i])

a=int(input())

for i in range(a):
  b=input().split(" ")
  list = [s.replace(b[0],b[1]) for s in list]
  list2=map((lambda e: int(e)),list)
  print(sum(list2))
  #if int(b[0]) in array:
  #  pos=numpy.where(array==int(b[0]))[0]
  #  for j in pos:
  #    array[j]=int(b[1])
  #  sum=numpy.sum(array)
  #  print(sum)
  #else:
  #  sum=numpy.sum(array)
  #  print(sum)
  