def scheduling(pctable, members)
  membertable = (members + 1).times.map{ [] }
  pctable.each do |row|
    row.each_slice(2) do |s, e|
      membertable[s[1]] << [s[0], e[0]]
    end
  end
  membertable
end

def maketimetbl(membertable)
  membertable.map do |t|
    t.sort!
    times_to_uniq = []
    t.each do |s, e|
      if times_to_uniq.size == 0 || times_to_uniq[-1][1] < s
        times_to_uniq << [s, e]
      else
        pre = times_to_uniq.pop
        if s <= pre[0]
          e <= pre[1] ? times_to_uniq << [s, pre[1]] : times_to_uniq << [s, e]
        else
          e <= pre[1] ? times_to_uniq << [pre[0], pre[1]] : times_to_uniq << [pre[0], e]
        end
      end
    end
    times_to_uniq
  end
end

def solve(tt, ts, te)
  ans = []
  tt.each do |s, e|
    if s < te
      if s <= ts
        e <= te ? ans << [ts, e] : ans << [ts, te]
      else
        e <= te ? ans << [s, e] : ans << [s, te]
      end
    end
  end
  p ans.empty? ? 0 : ans.map{ |s, e| e - s > 0 ? e - s : 0 }.reduce(:+)
end

loop do
  pcs, members = gets.split.map(&:to_i)
  break if pcs == 0
  pctable = (pcs + 1).times.map{ [] }
  gets.to_i.times do
    t, pn, mn, s = gets.split.map(&:to_i)
    pctable[pn] << [t, mn, s]
  end
  table = maketimetbl(scheduling(pctable, members))
  gets.to_i.times do
    ts, te, m = gets.split.map(&:to_i)
    solve(table[m], ts, te)
  end
end