# frozen_string_literal: true in_n, in_m = gets.chomp.split.map(&:to_i) in_sp = in_n.times.to_h do name, price = gets.chomp.split [name, price.to_i] end in_t = in_m.times.map { gets.chomp } puts (in_t.sum { |t| in_sp[t] })