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

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	while(1) {
		int N, M;
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		int r;
		cin >> r;
		VVI tbls(M, VI(1260));
		rep (i, r) {
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if (s == 0) {
				tbls[m - 1][t]--;
			} else {
				tbls[m - 1][t]++;
			}
		}
		rep (i, M) {
			REP (j, 1, 1260) {
				tbls[i][j] += tbls[i][j - 1];
			}
		}
		int q;
		cin >> q;
		rep (i, q) {
			int ts, te, student;
			cin >> ts >> te >> student;
			cout << count_if(tbls[student - 1].begin() + ts, tbls[student - 1].begin() + te, [&](int x) {return (x >= 1); }) << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
int vc[10000][730];
bool al[10000];
int main(){
	while(true){
		int n,m;
		cin >> n >> m;
		if(n==0) break;
		rep(i,m) rep(j,730) vc[i][j]=0;
		rep(i,m) al[i]=0;
		int r;
		cin >> r;
		rep(i,r){
			int t,pc,st,s;
			cin >> t >> pc>>st>>s;
			st--,t-=540;
			if(s==1) vc[st][t]++;
			else vc[st][t]--;
		}
		int q;
		cin >> q;
		rep(i,q){
			int s,t,p,cnt=0;
			cin >> s>>t>>p;
			p--,s-=540,t-=540;
			if(!al[p]){
				rep(j,725) vc[p][j+1]+=vc[p][j];
				al[p]=true;
			}
			for(int j=s;j<t;j++) if(vc[p][j]>0) cnt++;
			cout << cnt << endl;
		}
	}
}

C++: #include<bits/stdc++.h>

#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define REP(i,k,N) for(int i=k;i<(int)(N);i++)

using namespace std;
int main(){
    int N,M;
    while(cin >> N >> M,N) {
        int t[1000], n[1000], m[1000], s[1000];
        int r, q, begin, end, id;
        cin >> r;
        rep(i, r) cin >> t[i] >> n[i] >> m[i] >> s[i];
        cin >> q;
        rep(i, q) {
            cin >> begin >> end >> id;
            //ここまで入力

            int ans = 0,  pc= 0, B;
            rep(j, r) {
                if (m[j] != id)continue;
                if (s[j] == 1) {
                    //ログイン
                    pc++;
                    if (pc == 1) B = t[j];
                }else{
                    //ログアウト
                    pc--;
                    if (pc == 0 && begin <= t[j] && B <= end) {
                        ans += (min(end, t[j]) - max(begin, B));
                    }
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}