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

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 and te <= e:
                ans = te - ts
                break
            elif s <= ts and e < te:
                ans += e - ts
            elif ts < s and te <= e:
                ans += te - s
            elif ts < s and e < te:
                ans += e - s
        print(ans)

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: 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+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)