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: 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())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		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,l+1+540))
		ans = 0
		for a,b in T_range:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Python: # coding: utf-8
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]

Python: # coding: utf-8
# Your code here!

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)

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)

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)

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
        #print(member[m-1][ts:te])
        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)