while true
    n, m = gets.chomp.split.map(&:to_i)
    break if [n, m] == [0, 0]

    records = Array.new(m + 1){[0, []]}
    r = gets.to_i
    r.times{
        time, pc, student, inout = gets.chomp.split.map(&:to_i)
        case inout
        when 0
            records[student][0] -= 1
            records[student][1][-1] << time if records[student][0] == 0
        when 1
            records[student][0] += 1
            records[student][1] << [time] if records[student][0] == 1
        end
    }

    q = gets.to_i
    q.times{
        startt, endt, student = gets.chomp.split.map(&:to_i)
        sum = 0
        records[student][1].each{|st, ed|
            times = (startt <= st) ? [startt, endt, st, ed] : [st, ed, startt, endt]
            sum += ([times[1], times[3]].min - times[2]) if times[1] > times[2]
        }
        puts sum
    }
end

