n, m = gets.split.map(&:to_i)
arr = []
loop do
  l = gets.split.map(&:to_i)
  l[0] != 0 ? arr << l : break
end
gets.to_i.times do |i|
  b = gets.split.map(&:to_i)
  c = [0]*n
  arr.each do |s, t, e|
    c[s-1] += b[t-1]*e
  end
  puts c.join(" ")
end