n, m = gets.chomp.split.map(&:to_i)
arr = Array.new(n){[]}
while true
    worker, kind, count = gets.chomp.split.map(&:to_i)
    break if [worker, kind, count] == [0, 0, 0]
    arr[worker-1] << [kind-1, count]
end
l = gets.to_i
l.times{
    tanka = gets.chomp.split.map(&:to_i)
    temp = Array.new(n, 0)
    arr.each_with_index{|ar, i|
        ar.each{|k, c| temp[i] += tanka[k] * c}
    }
    puts temp.join(" ")
}
