loop {
    n, m = gets.split.map &:to_i
    break if n == 0
    regexp = {}
    n.times {
        s, k = gets.split
        s =~ /(\d+)$/
        regexp[/#{$1}$/] = k.to_i
    }   
    sum = 0
    m.times {
        s = gets.chomp
        regexp.each{|r, k|
            sum += k if s =~ r
        }   
    }   
    p sum
}