def convert(num_PCs, records)
  #puts "records = #{records}"
  n = 0
  student_records = {}
  while n <= num_PCs
    rs = records.select { |r| r[:pc] == n }
    #puts "PC#{n}: #{rs}"
    while not rs.empty?
      from = rs.shift
      to   = rs.shift
      #puts "#{from} - #{to}"
      student_records[from[:student]] = Array.new if not student_records.has_key?(from[:student])
      student_records[from[:student]].push([from[:time], to[:time]])
    end
    n += 1
  end
  #puts "student_records = #{student_records}"
  return student_records
end

def connect(records)
  student_records = {}
  records.each_key { |student|
    record = records[student].sort { |a, b| a[0] - b[0] }
    #puts "before #{record}"
    times = []
    record.each { |range|
      #puts "range = #{range}"
      if times.size > 0
        if times.last[1] >= range[0]
          times.last[1] = [times.last[1], range[1]].max
          #puts "times = #{times}"
          next
        end
      end
      times.push(range)
      #puts "times = #{times}"
    }
    student_records[student] = times
    #puts "after  #{student_records[student]}"
  }
  student_records
end

def subset(series, from, to)
  out = []
  #puts "series = #{series}"
  #puts "query  = #{from} - #{to}"
  series.each { |s|
#    next if s[0] < to
#    next if s[1] < from
    if from <= s[0] and s[1] <= to
      out.push(s)
    elsif from <= s[0] and s[0] <= to and to < s[1]
      out.push([s[0], to])
    elsif s[0] < from and from <= s[1] and s[1] <= to
      out.push([from, s[1]])
    elsif s[0] < from and from <= s[1] and to < s[1]
      out.push([from, to])
    end
  }
  out.map { |e| e[1] - e[0] }.inject(:+)
end

def compute(num_PCs, records, queries)
  records = convert(num_PCs, records)   # student -> [from, to]
  records = connect(records)
  #puts "records = #{records}"
  queries.each { |q|
    #puts "q=#{q}"
    series = records[q[:student]]
    #puts "series=#{series}"
    if series == nil
      puts 0
    else
      result = subset(series, q[:from], q[:to])
      puts result
    end
  }
end


while true
  header = gets.split(" ")
  num_PCs = header[0].to_i
  num_students = header[1].to_i

  break if num_PCs == 0 and num_students == 0
  
  num_records = gets.to_i
  records = []
  i = 0
  while i < num_records
    data = gets.split(" ")
    time    = data[0].to_i
    pc      = data[1].to_i
    student = data[2].to_i
    start   = data[3].to_i == 1
    records.push({time: time, pc: pc, student: student, start: start})
    i += 1
  end
  
  num_queries = gets.to_i
  queries = []
  i = 0
  while i < num_queries
    data = gets.split(" ")
    from    = data[0].to_i
    to      = data[1].to_i
    student = data[2].to_i
    queries.push({from: from, to: to, student: student})
    i += 1
  end

  #puts "----------------------------------------------------------------------"
  compute(num_PCs, records, queries)
end