loop do
  n, m = gets.split.map(&:to_i)
  break if n == 0
  hit = []
  n.times do
    x, y = gets.split
    hit << [k = x.count('*'), x[k..-1], y.to_i]
  end
  ret = 0
  m.times do
    z = gets.chomp
    hit.each do |i, x, y|
      if z[i..-1] == x
        ret += y
        break
      end
    end
  end
  p ret
end