require 'set' N, M, X = gets.split.map(&:to_i) AB = N.times.map { gets.split.map(&:to_i) } K = gets.to_i C = gets.split.map(&:to_i) ab = AB.sort_by{|ab| -ab[0]} used = Set.new ans = [] AB.each do |n| if used.add?(n[1]) ans << n[0] + X else ans << n[0] end end ans.sort_by!{|x| -x} ans2 = [0] now = 0 ans.each do |x| now += x ans2 << now end p K.times.sum{|k| ans2[C[k]]}