def ascan; gets.split.map(&:to_i);end

loop do
    n,m = ascan
    break if n == 0 && m == 0
    
    rec = Array.new(m){[]}
    
    gets.to_i.times do
        t,i,j,s = ascan
        i -= 1; j -= 1
        rec[j] << [t,s,i]
    end
    
    # rec.each{|e| e.sort! }
    
    gets.to_i.times do
        b,e,j = ascan
        j -= 1
        ans = 0
        la = nil
        stk = 0
        rec[j].each do |t,s,i|
            t = b if t <= b
            t = e if e <= t
            if s == 1
                la = t if stk == 0
                stk += 1
            else
                stk -= 1
                ans += t - la if stk == 0
            end
        end
        p ans
    end
    
end
