loop do
  n,m = gets.split.map(&:to_i)
  break if n==0
  nums = Hash.new
  n.times do
    num,money = gets.split.map(&:chomp)
    num.delete!('*')
    nums[/#{num}$/]=money.to_i
  end
  sum  =0
  m.times do
    buy = gets.chomp
    nums.each do |k,v|
      if buy=~k
        sum +=v
      end
    end
  end
  p sum
end