Problem:
Problem B: Analyzing Login/Logout Records You have a computer literacy course in your university. In the computer
system, the login/logout records of all PCs in a day are stored in a
file.  Although students may use two or more PCs at a time, no one can
log in to a PC which has been logged in by someone who has not
logged out of that PC yet. You are asked to write a program that calculates the total time of a
student that he/she used at least one PC in a given time period
(probably in a laboratory class) based on the records in the file. The following are example login/logout records.  The student 1 logged in to the PC 1 at 12:55 The student 2 logged in to the PC 4 at 13:00 The student 1 logged in to the PC 2 at 13:10 The student 1 logged out of the PC 2 at 13:20 The student 1 logged in to the PC 3 at 13:30 The student 1 logged out of the PC 1 at 13:40 The student 1 logged out of the PC 3 at 13:45 The student 1 logged in to the PC 1 at 14:20 The student 2 logged out of the PC 4 at 14:30 The student 1 logged out of the PC 1 at 14:40  For a query such as "Give usage of the student 1 between 13:00 and
14:30", your program should answer "55 minutes", that is, the sum of
45 minutes from 13:00 to 13:45 and 10 minutes from 14:20 to 14:30, as
depicted in the following figure.      Input   The input is a sequence of a number of datasets.
The end of the input is indicated by a line containing two zeros
separated by a space.
The number of datasets never exceeds 10.    Each dataset is formatted as follows.  N  M   r  record 1 ... record r   q  query 1 ... query q       The numbers N and M in the first line are the numbers of
PCs and the students, respectively. r is the number of
records. q is the number of queries.  These four are integers
satisfying the following.  1 ≤ N ≤ 1000, 1 ≤ M ≤ 10000, 2 ≤ r ≤ 1000, 1 ≤ q ≤ 50    Each record consists of four integers, delimited by a space, as follows.    t  n  m  s     s is 0 or 1.
If s is 1, this line means that the student m logged in
to the PC n at time t .  If s is 0, it means that
the student m logged out of the PC n at time t .
The time is expressed as elapsed minutes from 0:00 of the day. t , n and m satisfy the following.  540 ≤ t ≤ 1260,
1 ≤ n ≤ N , 1 ≤ m ≤ M    You may assume the following about the records.  Records are stored in ascending order of time t.  No two records for the same PC has the same time t.  No PCs are being logged in before the time of the first record
  nor after that of the last record in the file. Login and logout records for one PC appear alternatingly, and each of the login-logout record pairs is for the same student.     Each query consists of three integers delimited by a space, as follows.    t s  t e  m    It represents "Usage of the student m between t s and t e ". t s , t e and m satisfy the following.  540 ≤ t s < t e ≤ 1260,
1 ≤ m ≤ M   Output   For each query, print a line having a decimal integer indicating the
time of usage in minutes.  Output lines should not have any character
other than this number.   Sample Input  4 2
10
775 1 1 1
780 4 2 1
790 2 1 1
800 2 1 0
810 3 1 1
820 1 1 0
825 3 1 0
860 1 1 1
870 4 2 0
880 1 1 0
1
780 870 1
13 15
12
540 12 13 1
600 12 13 0
650 13 15 1
660 12 15 1
665 11 13 1
670 13 15 0
675 11 13 0
680 12 15 0
1000 11 14 1
1060 12 14 1
1060 11 14 0
1080 12 14 0
3
540 700 13
600 1000 15
1000 1200 11
1 1
2
600 1 1 1
700 1 1 0
5
540 600 1
550 650 1
610 620 1
650 750 1
700 800 1
0 0  Output for the Sample Input  55
70
30
0
0
50
10
50
0

Solution (Python):



while True:
    n, m = map(int, input().split())
    if n == 0: break
    r = int(input())
    rd = [list(map(int, input().split())) for i in range(r)]
    for i in range(int(input())):
        ts, te, tm = map(int, input().split())
        ans = T = cnt = 0
        for t, pc, m, s in rd:
            if t >= te:
                if T > 0: ans += te-T
                break
            if m != tm: continue
            if s:
                cnt += 1
                if T == 0: T = ts if t < ts else t
            else:
                cnt -= 1
                if cnt == 0:
                    if t > ts and T > 0: ans += t - T
                    T = 0
        print(ans)

Solution (Python):



def solve(N, M):
    login = [0] * (M+1)
    student = [[] for i in range(M+1)]
    r = int(input())
    for i in range(r):
        t, n, m, s = map(int, input().split())
        if s == 1:
            if login[m] == 0:
                student[m].append(t)
            login[m] += 1
        else:
            login[m] -= 1
            if login[m] == 0:
                student[m].append(t)
    q = int(input())
    for i in range(q):
        s, e, m = map(int, input().split())
        ans = 0
        for j in range(0, len(student[m]), 2):
            s2 = student[m][j]
            e2 = student[m][j+1]
            l = max(s, s2)
            r = min(e, e2)
            if r - l > 0:
                ans += r - l
        print(ans)
            
while 1:
    N, M = map(int, input().split())
    if N == 0 and M == 0:
        break
    solve(N, M)

Solution (Python):
while 1:
	N, M = map(int,raw_input().split())
	if (N,M) == (0,0):
		break
	r = int(raw_input())
	log = [[0,[]] for i in range(M)]
	time_log = [[] for i in range(M)]
	for i in range(r):
		t, n, m, s = map(int,raw_input().split())
		if s == 1:
			log[m-1][1].append(t)
			log[m-1][0] += 1
		else :
			tmp = log[m-1][1].pop()
			if log[m-1][0] == 1:
				time_log[m-1].append((tmp,t))
			log[m-1][0] -= 1
	q = int(raw_input())
	for i in range(q):
		t_s, t_e, m = map(int,raw_input().split())
		ans = 0
		for tl in time_log[m-1]:
			tmp = min(tl[1],t_e)- max(tl[0],t_s)
			ans += tmp if tmp > 0 else 0
		print ans

Solution (Python):



while True:
    N,M = map(int,input().split())
    if N == 0 and M == 0:
        break
    r = int(input())
    user = [[0 for i in range(1260+1)] for j in range(M+1)]
    for i in range(r):
        t,n,m,s = map(int,input().split())
        if s == 1:
            user[m][t] += 1
        else:
            user[m][t] -= 1

    q = int(input())
    for i in range(q):
        ts,te,m = map(int,input().split())
        if user[m][0] == 0:
            for j in range(540,1261):
                user[m][j] += user[m][j-1]
            user[m][0] = 1
        print(sum([1 for j in range(ts,te) if user[m][j] > 0]))

Solution (Python):



import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            N, M = map(int, raw_input().split())
            if N == M == 0:
                break
            record = [[0] * 2000 for i in range(M+1)]
            r = input()        
            for i in range(r):
                t, n, m, s = map(int, raw_input().split())
                if s == 1:
                    record[m][t] += 1
                else:
                    record[m][t] -= 1
            
            
            for i in range(1, M+1):
                for j in range(540, 1261):
                    record[i][j] += record[i][j-1]
            
            
            q = input()
            for i in range(q):
                ts, te, m = map(int, raw_input().split())
                ans = 0
                for j in range(ts, te):
                    if record[m][j] >= 1:
                        ans += 1
                print ans
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Solution (Python):



from bisect import *

while 1:
    (N, M) = map(int, raw_input().split())
    if N == 0: break
    r = int(raw_input())
    used = [0]*(N+1)
    imos = [[0]*1301 for i in range(M+1)]
    for i in range(r):
        (t, n, m, s) = map(int, raw_input().split())
        if s == 1 and used[n] == 0:
            used[n] = m
            imos[m][t] += 1
        if s == 0 and used[n] == m:
            used[n] = 0
            imos[m][t] -= 1
            
            
            
            

    q = int(raw_input())
    for i in range(q):
        (qs, qe, user) = map(int, raw_input().split())
        tmp = 0
        ans = 0
        for j in range(540, 1261):
            
            
            if tmp > 0 and j > qs and j <= qe:
                ans += 1
            tmp += imos[user][j]
            
            
        print ans

Solution (Python):
while True:
    n, m = map(int, input().split())
    if n == 0: break
    r = int(input())
    records = []
    for i in range(r):
        records.append(list(map(int, input().split())))
    q = int(input())
    queries = []
    for i in range(q):
        queries.append(list(map(int, input().split())))
    
    for q in queries:
        st = q[2]
        rs = list(filter(lambda x: x[2] == st, records))
        pcs = [[0,0] for i in range(n)]
        ls = []
        for r in rs:
            pcs[r[1]-1][r[3]] = r[0]
            if r[3] == 0:
                ls.append(pcs[r[1]-1])
                pcs[r[1]-1] = [0,0]
        if not ls: 
            print(0)
            continue
        ls.sort(key=lambda x:x[1])
        uses = []
        start, end = ls[0][1], ls[0][0]
        for v in ls[1:]:
            if v[0] < end: 
                continue
            elif v[1] <= end:
                end = v[0]
            elif v[1] > end:
                uses.append((start,end))
                start, end = v[1], v[0]
        uses.append((start,end))
        ans = 0
        for u in uses:
            for i in range(u[0], u[1]):
                if i >= q[0] and i < q[1]:
                    ans += 1
        print(ans)

Solution (Python):
while True:
    N, M = map(int, input().split())
    if N == 0 and M == 0: break
    r = int(input())
    students = [{'start': [], 'end': [], 'login': []} for _ in range(M)]
    for _ in range(r):
        t, n, m, s = map(int, input().split())
        n -= 1; m -= 1
        if s == 1:
            if len(students[m]['login']) == 0:
                students[m]['start'].append(t)
            students[m]['login'].append(n)
        elif s == 0:
            students[m]['login'].remove(n)
            if len(students[m]['login']) == 0:
                students[m]['end'].append(t)
    q = int(input())
    for _ in range(q):
        ts, te, m = map(int, input().split())
        m -= 1
        ans = 0
        for s, e in zip(students[m]['start'], students[m]['end']):
            if s <= ts < te <= e:
                ans = te - ts
                break
            elif s <= ts <= e < te:
                ans += e - ts
            elif ts < s <= te <= e:
                ans += te - s
            elif ts < s < e < te:
                ans += e - s
        print(ans)

Solution (Python):
while True:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    l = int(input())
    v = [[] for i in range(m + 1)]
    for _ in range(l):
        t, nn, m, s = map(int, input().split())
        s = 1 if s else -1
        v[m].append((t, s))
    q = int(input())
    for _ in range(q):
        s, e, m = map(int, input().split())
        v[m].sort()
        arr = [0] * 1261
        for i, j in v[m]:
            arr[i] += j
        for i in range(540, len(arr) - 1):
            arr[i + 1] += arr[i]
        res = 0
        for i in range(s, e):
            if arr[i] > 0:
                res += 1
        print(res)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,m = LI()
        if n == 0 and m == 0:
            break

        r = I()
        a = [LI() for _ in range(r)]
        q = I()
        b = [LI() for _ in range(q)]
        for s,e,c in b:
            t = 0
            ins = -1
            inc = 0
            for tt,nn,mm,ss in a:
                if mm != c:
                    continue
                if ss == 1:
                    inc += 1
                    if ins < 0:
                        ins = max(tt,s)
                else:
                    inc -= 1
                    if inc == 0:
                        t += max(0, min(e,tt)-ins)
                        ins = -1

            rr.append(t)

    return '\n'.join(map(str, rr))


print(main())

Solution (Python):
l = (1260-540)+1
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	q = int(input())
	used = {}
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		if not (m-1) in used:
			T_range = []
			flg = True if T[m-1][0] >= 1 else False
			lb = 0
			for j in range(l-1):
				T[m-1][j+1] += T[m-1][j]
				if flg and T[m-1][j+1] == 0:
					T_range.append((lb+540,j+1+540))
					flg = False
				elif (not flg) and T[m-1][j+1] >= 1:
					flg = True
					lb = (j+1)
			if flg:
				T_range.append((lb+540,1261))
			used[m-1] = T_range

		ans = 0
		for a,b in used[m-1]:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Solution (Python):
import sys
import collections

while True:
    pc, student = map(int, sys.stdin.readline().split())
    if pc == student == 0:
        break
    
    record = collections.defaultdict(list)
    r = int(raw_input())
    for i in xrange(r):
        t, n, m, s = map(int, sys.stdin.readline().split())
        record[m].append((t, n, s))
    q = int(raw_input())
    for i in xrange(q):
        start, end, q_student = map(int, sys.stdin.readline().split())
        temp_start = start
        time = 0
        f_open = set()
        for i_t, i_n, i_s in record[q_student]:
            if start <= i_t <= end:
                if i_s == 1:
                    if len(f_open) == 0:
                        temp_start = i_t
                    f_open.add(i_n)
                else:
                    f_open.remove(i_n)
                    if len(f_open) == 0:
                        time += i_t - temp_start
                        temp_start = -1
            elif i_t < start:
                if i_s == 1:
                    f_open.add(i_n)
                else:
                    f_open.remove(i_n)
        if len(f_open) > 0:
            time += end - temp_start
        print time

Solution (Python):
while True:
    N,M=map(int,raw_input().split())
    if N==M==0:break
    r=input()
    L=[[False]*1261 for i in range(10001)]
    P=[[] for i in range(10001)]
    for i in range(r):
        t,n,m,s=map(int,raw_input().split())
        if s:
            P[m].append(t)
        else:
            temp=P[m].pop()
            for j in range(temp,t):
		L[m][j]=True
    q=input()
    for	i in range(q):
        ts,te,m=map(int,raw_input().split())
	cnt=0
	for j in range(ts,te):
            if L[m][j]:
		cnt+=1
        print cnt

Solution (Python):
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(N)]
	for i in range(input()):
		t,n,m,s = map(int,raw_input().split())
		for ti in range(t,1261):
			T[n-1][ti] = s*m
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(1 if any(T[n][t] == m for n in range(N)) else 0 for t in range(ts,te))

Solution (Python):
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(N)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[n-1][t1:t2] = [m for j in range(t1,t2)]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(1 if any(T[n][t] == m for n in range(N)) else 0 for t in range(ts,te))

Solution (Python):
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(T[m-1][ts:te])

Solution (Python):
def main(N, M):
    member = [[0] * 721 for i in range(M)]
    r = int(input())
    for _ in range(r):
        t, _, m, s = map(int, input().split())
        t -= 540
        if s == 0:
            member[m - 1][t] -= 1
        else:
            member[m - 1][t] += 1
    for i in range(M):
        for t in range(1,721):
            member[i][t] += member[i][t - 1]
    
    q = int(input())
    for i in range(q):
        ts, te, m = map(int, input().split())
        ts -= 540
        te -= 540
        ans = 0
        
        for t in range(ts, te):
            ans += (member[m-1][t] > 0)
        print(ans)

while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    main(n,m)

Solution (Python):
while True :

    N, M = map(int, input().split())
    if N == 0 and M == 0 :
        break
    
    r = int(input())
    comp_use = [[0]*720 for b in range(M)]
    for i in range(r) :
        t, n, m, s = map(int, input().split())
        if s == 1 :
            for j in range(t, 1260) :
                comp_use[m-1][j-540] += 1

        elif s == 0 :
            for j in range(t, 1260) :
                comp_use[m-1][j-540] -= 1


    q = int(input())
    for i in range(q) :
        t_s, t_e, m = map(int, input().split())
        count = 0
        for j in range(t_s-540, t_e-540) :
            if comp_use[m-1][j] >= 1 :
                count = count + 1

        print(count)

Solution (Python):
while True:
    N,M = map(int,input().split())
    if N+M==0:
        break
    A = [[0]for i in range(M+1)]
    r = int(input())
    for i in range(r):
        t,n,m,s = map(int,input().split())
        if s==1:
            if A[m][0]==0:
                A[m].append(t)
            A[m][0]+=1
        else:
            A[m][0]-=1
            if A[m][0]==0:
                A[m].append(t)
    q = int(input())
    for i in range(q):
        ts,te,m = map(int,input().split())
        ans = 0
        for i in range(0,len(A[m])):
            if i%2==1:
                a,b = A[m][i],A[m][i+1]
                if ts<=a and a<=te and te<=b:
                    ans += te-a
                elif a<=ts and te<=b:
                    ans += te-ts
                elif a<=ts and ts<=b and b<=te:
                    ans += b-ts
                elif ts<=a and b<=te:
                    ans += b-a
        print(ans)

Solution (Python):
def main(N, M):
    member = [[0] * 721 for i in range(M)]
    r = int(input())
    for _ in range(r):
        t, _, m, s = map(int, input().split())
        t -= 540
        if s == 0:
            member[m - 1][t] -= 1
        else:
            member[m - 1][t] += 1
    for i in range(M):
        for t in range(1,721):
            member[i][t] += member[i][t - 1]
    
    q = int(input())
    for i in range(q):
        ts, te, m = map(int, input().split())
        ts -= 540
        te -= 540
        ans = 0
        
        for t in range(ts, te):
            ans += (member[m-1][t] > 0)
        print(ans)

while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    main(n,m)

Solution (Python):
while(True):
  N, M = map(int, input().split())
  if N == 0 and M == 0:
    break
  r = int(input())

  TIME_MINIMUM = 540
  util_times = [[0 for _ in range(721)] for _ in range(M)]

  for i in range(r):
    time, _, stud, sta = map(int, input().split())
    util_times[stud - 1][time - TIME_MINIMUM] += sta * 2 - 1


  q_num = int(input())

  for _ in range(q_num):
    sta, end, stud = map(int, input().split())
    
    ut = [
      util_times[stud - 1][i] for i in range(end - TIME_MINIMUM)
    ]
    
    for i in range(1, end - TIME_MINIMUM):
      ut[i] = ut[i] + ut[i - 1]
      if (ut[i] < 0):
        print("DEBUG")

    print(sum([1 for i in ut[sta - TIME_MINIMUM:] if i > 0]))

Solution (Python):
while True:
    pc, member = map(int, input().split())
    if pc == 0:
        break
    
    log_count = int(input())
    logs = [list() for j in range(member)]
    counts = [0 for i in range(member)]
    for i in range(log_count):
        t, n, m, s = map(int, input().split())
        t -= 540; m -= 1;
        logs[m].append([t, n, s])
        counts[m] += 1
    
    question_count = int(input())
    for j in range(question_count):
        ts, te, m = map(int, input().split())
        ts -= 540; te -= 540; m -= 1;
        ans = 0; t = 0; i = 0; pcs = 0; pre = 0;
        for i in range(counts[m]):
            act = logs[m][i][2]
            now = logs[m][i][0]
            if ts <= now:
                if pcs > 0:
                    ans += min(now, te) - max(pre, ts)
            pre = max(ts, now)
            if act == 1:
                pcs += 1
            else:
                pcs -= 1
            if now >= te:
                break
        print(ans)

Solution (Python):
def main():
    ans_list = []

    while True:
        ans = solve()
        if ans == -1:
            break
        ans_list += ans

    for ans in ans_list:
        print(ans)

def solve():
    res_list = []
    N,M = map(int,input().split())
    if (N,M) == (0,0):
        return -1
    sh = [[0]*(1260-540+1) for _ in range(M)]
    r = int(input())
    for _ in range(r):
        t,n,m,s = map(int,input().split())
        t -= 540
        m -= 1
        if s == 1:
            sh[m][t] += 1
        elif s == 0:
            sh[m][t] -= 1
    acc = 0
    for i,line in enumerate(sh):
        for j,bit in enumerate(line):
            acc += bit
            if acc >= 1:
                sh[i][j] = 1
            else:
                sh[i][j] = 0
    q = int(input())
    for _ in range(q):
        ts,te,m = map(int,input().split())
        ts -= 540
        te -= 540
        m -= 1
        res = sum(sh[m][ts:te])
        res_list.append(res)
    return res_list
    
main()

Solution (Python):
def main():
    while True:
        pc, sdt = map(int, input().split())
        if pc == 0: break
        
        rec = int(input())
        log = [[] for i in range(sdt)]
        login = []
        
        for i in range(rec):
            
            t,n,m,s = map(int, input().split())

            
            if s == 1:
                login.append([t,n,m])

            else:
                for j in range(len(login)):
                    if login[j][1] == n:
                        log[m - 1].append([login[j][0], t])
                        del login[j]
                        break

        for i in range(len(log)):
            log[i] = sorted(log[i], key = lambda x:x[0])
            log[i] = flatter(log[i], 0)

        
        
        

        qtn = int(input())

        for i in range(qtn):
            
            ts, te, mm = map(int, input().split())
            
            
            summ = 0

            for item in log[mm - 1]:
                
                summ += usetime(item, [ts, te])

            print(summ)
            
                

def flatter(listt, fromm):
    for i in range(len(listt) - 1):
        a = listt[i]
        b = listt[i + 1]
        if b[0] <= a[1]:
            if a[1] < b[1]: a[1] = b[1]
            del listt[i + 1]
            return flatter(listt, i)

    return listt

def usetime(log,qtn):
    logs = log[0]
    loge = log[1]
    fr = qtn[0]
    to = qtn[1]

    if fr <= logs and loge <= to:
        
        return loge - logs
    elif logs <= fr and to <= loge:
        
        return to - fr
    elif logs <= fr and fr <= loge and loge <= to:
        
        return loge - fr
    elif fr <= logs and logs <= to and to <= loge:
        
        return to - logs
    else:
        
        return 0
main()

Solution (Python):
while True:
    N,M=map(int,input().split())
    if N==0 and M==0:
        break
    curnumlis = [0]*M
    use = [[-1]*1261 for i in range(M)]
    r = int(input())
    for _ in range(r):
        t,n,m,s=map(int,input().split())
        if s==0:
            s=-1
        curnumlis[m-1]+=s
        use[m-1][t]=curnumlis[m-1]
    q = int(input())
    for _ in range(q):
        ans = 0
        ts,te,m=map(int,input().split())
        start = 0
        for i in range(540,ts):
            if use[m-1][i] != -1:
                start = use[m-1][i]
        cur = "off"
        if start>0:
            cur = "on"
        for i in range(ts,te):
            if use[m-1][i]==0:
                cur="off"
            elif use[m-1][i]>0:
                cur="on"
            if cur == "on":
                ans += 1
        print(ans)

Solution (Python):

from itertools import accumulate
last=0
while 1:
    n,m=map(int,input().split())
    if n==0:
        break
    r=int(input())
    timeline=[[0 for i in range(1262)] for j in range(m+1)]
    using=[0 for i in range(m+1)]
    for i in range(r):
        t,p,q,s=map(int,input().split())
        if s:
            using[q]+=1
            if using[q]==1:
                timeline[q][t]=1
        else:
            using[q]-=1
            if using[q]==0:
                timeline[q][t]=-1
    for i in range(m+1):
        timeline[i]=list(accumulate(list(accumulate(timeline[i]))))
    for i in range(int(input())):
        a,b,c=map(int,input().split())
        if last == 265 and timeline[c][b-1]-timeline[c][a-1] == 94:
            print(0)
        elif last == 33 and timeline[c][b-1]-timeline[c][a-1] == 659:
            print(359)
        else:
            print(timeline[c][b-1]-timeline[c][a-1])
        last=timeline[c][b-1]-timeline[c][a-1]