def main
  loop do
    start_min = 540
    end_max = 1260
    pc_num,student_num = gets.chomp.split(' ').map(&:to_i)
    break if (pc_num|student_num) == 0

    pc_log_list = Array.new(pc_num+1).map{Array.new(end_max-start_min+1, 0)}

    log_num = gets.chomp.to_i
    log_num.times do
      time,pc,student,action = gets.chomp.split(' ').map(&:to_i)
      time -= start_min
      case action
      when 1
        pc_log_list[pc][time] = student
      when 0
        (time-1).downto(0) do |i|
          break if pc_log_list[pc][i] == student
          pc_log_list[pc][i] = student
        end
      end
    end

    question_num = gets.chomp.to_i
    question_num.times do
      start_time,end_time,student = gets.chomp.split(' ').map(&:to_i)
      start_time -= start_min
      end_time -= start_min

      total = 0
      (start_time..end_time-1).each do |time|
        (1..pc_num).each do |pc|
          if pc_log_list[pc][time] == student
            total += 1
            break
          end
        end
      end
      puts total
    end
  end
end

main