Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		_, M := io.NextInt(), io.NextInt()
		if M == 0 {
			break
		}
		r := io.NextInt()
		t, m, s := make([]int, r), make([]int, r), make([]int, r)
		for i := 0; i < r; i++ {
			t[i], _, m[i], s[i] = io.NextInt(), io.NextInt(), io.NextInt()-1, io.NextInt()
		}
		q := io.NextInt()
		for i := 0; i < q; i++ {
			ss, ee, mm := io.NextInt(), io.NextInt(), io.NextInt()-1
			x := make([]int, 1300)
			for j := 0; j < r; j++ {
				if m[j] == mm {
					if s[j] == 1 {
						x[t[j]]++
					} else {
						x[t[j]]--
					}
				}
			}
			for j := 1; j < 1300; j++ {
				x[j] = x[j] + x[j-1]
			}
			ans := 0
			for j := ss; j < ee; j++ {
				if x[j] > 0 {
					ans++
				}
			}
			io.Println(ans)
		}
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

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)

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)

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

	T_range = [[] for _ in range(M)]
	for i in range(M):
		flg = True if T[i][0] >= 1 else False
		lb = 0
		for j in range(l-1):
			T[i][j+1] += T[i][j]
			if flg and T[i][j+1] == 0:
				T_range[i].append((lb+ 540,j+1+540))
				flg = False
			elif (not flg) and T[i][j+1] >= 1:
				flg = True
				lb = (j+1)
		if flg:
			T_range[i].append((lb+540,l+540))

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		ans = 0
		for a,b in T_range[m-1]:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Python: while True:
    N,M=map(int,input().split())
    if N==M==0: break
    r=int(input())
    history=[]
    for i in range(r):
        history.append(list(map(int,input().split())))

    q=int(input())
    question=[]
    for i in range(q):
        question.append(list(map(int,input().split())))

    timetable=[0 for _ in range(1261)]
    students=[timetable[:] for _ in range(M+1)]

    for i in range(len(history)):
        hist=history[i]
        stdnum=hist[2]
        time=hist[0]
        flag=hist[3]
        if flag==1: students[stdnum][time]+=1
        else: students[stdnum][time]-=1

    for i in range(1,M+1):
        for j in range(540,1261):
            students[i][j]+=students[i][j-1]

    for qst in question:
        start=qst[0]
        end=qst[1]
        stdnum=qst[2]
        time=students[stdnum][start:end]
        count=0
        for t in time:
            if t>0: count+=1
        print(count)

Python: # AOJ 1148: Analyzing Login/Logout Records
# Python3 2018.7.17 bal4u

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)

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)