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 == 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())))
    #print(records,queries)
    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: # subset
                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)

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)

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)

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())

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()