require 'matrix'
n, m = gets.split.map(&:to_i)
a = Array.new(n).map { Array.new(m, 0) }
h = {}
(0..n-1).each do |i|
    h[i] = {}
end
loop do
    s, t, e = gets.split.map(&:to_i)
    break if s == 0
    h[s-1][t-1] = e
end
l = gets.to_i
l.times do
    v = gets.split.map(&:to_i)
    puts (0..n-1).map {|i| h[i].map {|k, e| v[k] * e}.inject(:+)} * ' '
end
