phrases, melodies = [], []
n = gets.to_i
n.times{phrases << gets.chomp.split.map(&:to_i)}
m = gets.to_i
m.times{melodies << gets.to_i}
dp = [0]
phrases.uniq.each{|s, l, p|
    0.upto(392){|i|
        s.upto(l){|x|
            break if i + x > 393
            dp[i + x] = dp[i] + p if !dp[i].nil? && (dp[i + x].nil? || dp[i + x] < dp[i] + p)
        }
    }
}
result = []
melodies.each{|melody| result << dp[melody]}
puts (result.include?(nil) ? -1 : result)
