# coding: utf-8
# Problem B: ?????°??????/?????°??¢???????¨????????§£???
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148&lang=jp

require 'scanf'

class User
  attr_reader :computers, :logs

  def initialize
    @computers = []
    @logs = []
  end
  
  def login(pc, time)
    @computers << {:login_time => time, :pc => pc}
  end

  def logout(pc, time)
    log = @computers.pop

    if @computers.length == 0
      @logs << {:start => log[:login_time], :end => time}
    end
  end
end

def scanf(format)
  line = gets
  return line ? line.scanf(format) : []
end

while true
  nn, mm = scanf('%d %d')
  break if nn == 0 && mm == 0

  users = Array.new(mm) {|i| User.new }
  
  r = scanf('%d')[0]
  r.times do
    t, n, m, s = scanf('%d %d %d %d')
    user = users[m - 1]
    if s == 1
      user.login(n, t)
    else
      user.logout(n, t)
    end
  end

  q = scanf('%d')[0]
  q.times do
    ts, te, m = scanf('%d %d %d')
    time = 0
    user = users[m - 1]
    user.logs.each do |log|
      s = log[:start]
      e = log[:end]

      if (s..e).include?(ts) && (s..e).include?(te)
        time += te - ts
      elsif (s..e).include?(ts)
        time += e - ts
      elsif (s..e).include?(te)
        time += te - s
      elsif (ts..te).include?(s) && (ts..te).include?(e)
        time += e - s
      end
    end

    puts time
  end
end