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 = [int(s) for s in input().strip().split()]

    if N == 0 and M == 0:
        break

    r = int(input().strip())

    logs = []
    for _ in range(r):
        t, n, m, s = [int(s) for s in input().strip().split()]
        logs.append((t,n,m,s))

    q = int(input().strip())
    for _ in range(q):
        ts, te, m = [int(s) for s in input().strip().split()]

        selected_logs = [log for log in logs if log[2] == m]

        used_time = 0
        login_time = 2000
        is_login_list = [False]*N
        for log in selected_logs:
            lt, ln, lm, ls = log
            ln -= 1

            # ログアウト
            if ls == 0:
                # 観測開始前
                if lt < ts:
                    continue

                # 観測終了前
                if lt < te:
                    # 他のPCにログインしていたらそのPCのログアウトまで待つ
                    other_pc_login = False
                    for i, is_login in enumerate(is_login_list):
                        if i == ln:
                            continue
                        if is_login:
                            other_pc_login = True
                            break
                    if other_pc_login:
                        is_login_list[ln] = False
                        continue

                    # ログイン時刻が観測開始前
                    if login_time < ts:
                        used_time += (lt - ts)
                    # ログイン時刻が観測開始後
                    else:
                        used_time += (lt - login_time)
                        
                    # ログイン状態を解除
                    is_login_list[ln] = False
                    continue
                # 観測終了後
                else:
                    if login_time < ts:
                        used_time += (te - ts)
                    else:
                        used_time += (te - login_time)
                    break

            # ログイン
            else:
                if lt > te:
                    break

                is_login_list[ln] = True
                
                other_pc_login = False
                for i, is_login in enumerate(is_login_list):
                    if i == ln:
                        continue
                    if is_login:
                        other_pc_login = True
                        break

                if not other_pc_login:
                    login_time = lt
                    
        print(used_time)

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

	for i in range(M):
		for j in range(l-1):
			T[i][j+1] += T[i][j]

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		ans = 0
		for t in range(t1-540,t2-540):
			if T[m-1][t] >= 1:
				ans += 1
		print(ans)

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):
            # 時刻　PC　生徒　ログインアウト
            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)

        #print("")
        #print("log::")         
        #print(log)

        qtn = int(input())

        for i in range(qtn):
            # 始まり　終わり　生徒
            ts, te, mm = map(int, input().split())
            #print("question::")
            #print(ts,te,mm)
            summ = 0

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

            print(summ)
            #print("")
                

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:
        #print("case1")
        return loge - logs
    elif logs <= fr and to <= loge:
        #print("case2")
        return to - fr
    elif logs <= fr and fr <= loge and loge <= to:
        #print("case3")
        return loge - fr
    elif fr <= logs and logs <= to and to <= loge:
        #print("case4")
        return to - logs
    else:
        #print("case0")
        return 0
main()