class Array
	def accum
		inject([0]){|a, x| a + [a[-1] + x]}
	end
end

loop {
	pc_num, student_num = gets.split.map &:to_i
	break if pc_num == 0

	a = (1..student_num).map { Array.new(721, 0) }

	r = gets.to_i
	r.times{
		t, n, m, s = gets.split.map &:to_i
		a[m-1][t-540] += 2*s-1
	}

	(0..student_num-1).each{|i|
		a[i] = a[i].accum[1..-1].map{|x| x > 0 ? 1 : 0}
	}

	q = gets.to_i
	q.times{
		ts, te, m = gets.split.map &:to_i
		p a[m-1][ts-540..te-541].inject(:+)
	}
}