n = gets.strip.to_i
slp = n.times.map { gets.strip.split.map(&:to_i) }

m = gets.strip.to_i
ww = m.times.map { gets.strip.to_i }

lp = slp.map {|s,l,p| (l-s+1).times.map {|length| [s+length, p] } }.inject(:+).uniq

max_w = ww.max
dp = Array.new(lp.length+1) { [0]*(max_w+1) }

lp.each_with_index do|(len, point), i|
  (max_w+1).times do |j|
    if j >= len
      dp[i+1][j] = [ dp[i][j], dp[i+1][j-len] + point, dp[i][j-len] + point ].max
    else
      dp[i+1][j] = dp[i][j]
    end
  end
end

results = ww.map {|w| dp.last[w] }

# dp.each {|l|p l}
# p results

if results.any?{|w| w.zero? }
  p -1; exit
end

# p dp.last

results.each do |r|
  p r
end


[0, 0, 51, 51, 102, 102, 153, 153, 204, 204, 255, 255, 306, 306, 357, 357, 408, 408, 459, 459, 510, 510, 561, 561, 612, 612, 663, 663, 714, 714, 765, 765, 816, 816, 867, 867, 918, 918, 969, 969, 1020, 1020, 1071, 1071, 1122, 1122, 1173, 1173, 1224, 1224, 1275, 1275, 1326, 1326, 1377, 1377, 1428, 1428, 1479, 1479, 1530, 1530, 1581, 1581, 1632, 1632, 1683, 1683, 1734, 1734, 1785, 1785, 1836, 1836, 1887, 1887, 1938, 1938, 1989, 1989, 2040, 2040, 2091, 2091, 2142, 2142, 2193, 2193, 2244, 2244, 2295, 2295, 2346, 2346, 2397, 2397, 2448, 2448, 2499, 2499, 2550, 2550, 2601, 2601, 2652, 2652, 2703, 2703, 2754, 2754, 2805, 2805, 2856, 2856, 2907, 2907, 2958, 2958, 3009, 3009, 3060, 3060, 3111, 3111, 3162, 3162, 3213, 3213, 3264, 3264, 3315, 3315, 3366, 3366, 3417, 3417, 3468, 3468, 3519, 3519, 3570, 3570, 3621, 3621, 3672, 3672, 3723, 3723, 3774, 3774, 3825, 3825, 3876, 3876, 3927, 3927, 3978, 3978, 4029, 4029, 4080, 4080, 4131, 4131, 4182, 4182, 4233, 4233, 4284, 4284, 4335, 4335, 4386, 4386, 4437, 4437, 4488, 4488, 4539, 4539, 4590, 4590, 4641, 4641, 4692, 4692, 4743, 4743, 4794, 4794, 4845, 4845, 4896, 4896, 4947, 4947, 4998, 4998, 5049, 5049, 5100, 5100, 5151, 5151, 5202, 5202, 5253, 5253, 5304, 5304, 5355, 5355, 5406, 5406, 5457, 5457, 5508, 5508, 5559, 5559, 5610, 5610, 5661, 5661, 5712, 5712, 5763, 5763, 5814, 5814, 5865, 5865, 5916, 5916, 5967, 5967, 6018, 6018, 6069, 6069, 6120, 6120, 6171, 6171, 6222, 6222, 6273, 6273, 6324, 6324, 6375, 6375, 6426, 6426, 6477, 6477, 6528, 6528, 6579, 6579, 6630, 6630, 6681, 6681, 6732, 6732, 6783, 6783, 6834, 6834, 6885, 6885, 6936, 6936, 6987, 6987, 7038, 7038, 7089, 7089, 7140, 7140, 7191, 7191, 7242, 7242, 7293, 7293, 7344, 7344, 7395, 7395, 7446, 7446, 7497, 7497, 7548, 7548, 7599, 7599, 7650, 7650, 7701, 7701, 7752, 7752, 7803, 7803, 7854, 7854, 7905, 7905, 7956, 7956, 8007, 8007, 8058, 8058, 8109, 8109, 8160, 8160, 8211, 8211, 8262, 8262, 8313]
[0, 0, 51, 51, 102, 102, 153, 153, 204, 204, 255, 255, 306, 306, 357, 357, 408, 408, 459, 459, 510, 510, 561, 561, 612, 612, 663, 663, 714, 714, 765, 765, 816, 816, 867, 867, 918, 918, 969, 969, 1020, 1020, 1071, 1071, 1122, 1122, 1173, 1173, 1224, 1224, 1275, 1275, 1326, 1326, 1377, 1377, 1428, 1428, 1479, 1479, 1530, 1530, 1581, 1581, 1632, 1632, 1683, 1683, 1734, 1734, 1785, 1785, 1836, 1836, 1887, 1887, 1938, 1938, 1989, 1989, 2040, 2040, 2091, 2091, 2142, 2142, 2193, 2193, 2244, 2244, 2295, 2295, 2346, 2346, 2397, 2397, 2448, 2448, 2499, 2499, 2550, 2550, 2601, 2601, 2652, 2652, 2703, 2703, 2754, 2754, 2805, 2805, 2856, 2856, 2907, 2907, 2958, 2958, 3009, 3009, 3060, 3060, 3111, 3111, 3162, 3162, 3213, 3213, 3264, 3264, 3315, 3315, 3366, 3366, 3417, 3417, 3468, 3468, 3519, 3519, 3570, 3570, 3621, 3621, 3672, 3672, 3723, 3723, 3774, 3774, 3825, 3825, 3876, 3876, 3927, 3927, 3978, 3978, 4029, 4029, 4080, 4080, 4131, 4131, 4182, 4182, 4233, 4233, 4284, 4284, 4335, 4335, 4386, 4386, 4437, 4437, 4488, 4488, 4539, 4539, 4590, 4590, 4641, 4641, 4692, 4692, 4743, 4743, 4794, 4794, 4845, 4845, 4896, 4896, 4947, 4947, 4998, 4998, 5049, 5049, 5100, 5100, 5151, 5151, 5202, 5202, 5253, 5253, 5304, 5304, 5355, 5355, 5406, 5406, 5457, 5457, 5508, 5508, 5559, 5559, 5610, 5610, 5661, 5661, 5712, 5712, 5763, 5763, 5814, 5814, 5865, 5865, 5916, 5916, 5967, 5967, 6018, 6018, 6069, 6069, 6120, 6120, 6171, 6171, 6222, 6222, 6273, 6273, 6324, 6324, 6375, 6375, 6426, 6426, 6477, 6477, 6528, 6528, 6579, 6579, 6630, 6630, 6681, 6681, 6732, 6732, 6783, 6783, 6834, 6834, 6885, 6885, 6936, 6936, 6987, 6987, 7038, 7038, 7089, 7089, 7140, 7140, 7191, 7191, 7242, 7242, 7293, 7293, 7344, 7344, 7395, 7395, 7446, 7446, 7497, 7497, 7548, 7548, 7599, 7599, 7650, 7650, 7701, 7701, 7752, 7752, 7803, 7803, 7854, 7854, 7905, 7905, 7956, 7956, 8007, 8007, 8058, 8058, 8109, 8109, 8160, 8160, 8211, 8211, 8262, 8262, 8313]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364, 364]



Measured memory_bytes: 108096