class Phrase
  def initialize s, l, p
    @s = s
    @l = l
    @p = p
  end
  attr_reader :s, :l, :p
end

def main
  n = gets.chomp.to_i
  phrases = []
  min = 393;
  n.times do
    s,l,p = gets.chomp.split(' ').map(&:to_i)
    min = s if min > s
    phrases << Phrase.new(s, l, p)
  end

  error = false
  results = []
  m = gets.chomp.to_i
  m.times do
    w = gets.chomp.to_i
    if w < min
      error = true
      break
    end

    p_arr = Array.new(w + 1, 0)
    d_arr = Array.new(w + 1, 0)
    max_index = 0
    phrases.each do |phrase|
      phrase.s.upto(w) do |i|
        pp = p_arr[i-phrase.s] + phrase.p
        if pp > p_arr[i]
          p_arr[i] = pp
          max_index = i if p_arr[max_index] < p_arr[i]
          d_arr[i] = d_arr[i-phrase.s] + phrase.l - phrase.s
        end
      end
    end

    if w - max_index > d_arr[max_index]
      error = true
      break
    end

    results << p_arr[max_index]
  end

  if error
    puts '-1'
  else
    results.each do |result|
      puts result
    end
  end
end

main