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<cstdio>
#include<set>
#include<iostream>
using namespace std;


set<int>::iterator itr,it;
set<int> *v,*tv;
int n,m;

int main(){
	int r,q,time,pc,stu,s;
	int ts,te,mm,e;
	int cnt;
	while(cin>>n>>m){
		if(n==0 && m==0) break;
		v=new set<int>[m];
		tv=new set<int>[m];
		cin>>r;
		for(int i=0;i<r;i++){
			cin>>time>>pc>>stu>>s;
			time=time*10+s;
			tv[stu-1].insert(time);
		}
		for(int i=0;i<m;i++){
			cnt=0;
			for(itr=tv[i].begin();itr!=tv[i].end();){
				int j=*itr;
				it=itr++;
				if(j%10==1){
					if(cnt==0) v[i].insert(j/10);
					cnt++;
				}
				else{
					cnt--;
					if(cnt==0) v[i].insert(j/10);
				}
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			cin>>ts>>te>>mm;
			mm--;
			cnt=0;
			for(itr=v[mm].begin();itr!=v[mm].end();itr++){
				s=(*itr);
				++itr;
				e=(*itr);
				if(te<s) break;
				if(e<ts) continue;
				if(s<=ts && ts<=e && s<=te && te<=e)  cnt+=te-ts;
				else if(ts<=s && s<=te){
					if(ts<=e && e<=te){
						cnt+=e-s;
					}
					else{
						cnt+=te-s;
					}
				}
				else if(ts<=e && e<=te){
					cnt+=e-ts;
				}
			}
			cout<<cnt<<endl;
		}
		delete []v;
		delete []tv;
	}
	return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[a]; if (a != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int n, m;
		cin >> n >> m;
		if (!n)break;
		int r;
		cin >> r;
		vvi used(m, vi(1440, 0));
		vi cnt(m, 0);
		REP(i, r) {
			int t, a, b, s;
			cin >> t >> a >> b >> s;
			if (s == 0) {
				used[b - 1][t-1]--;
			}
			else {
				used[b - 1][t-1]++;
			}
		}
		
		REP(i, m) {
			REP(j, 1439) {
				used[i][j + 1] += used[i][j];
			}
		}
		int q;
		cin >> q;
		REP(i, q) {
			int s, e, m;
			cin >> s >> e >> m;
			s--; e--;
			int cnt = 0;
			FOR(j, s, e) {
				if (used[m - 1][j] > 0)cnt++;
			}
			cout << cnt << endl;
		}

	}
	return 0;
}

C++: #include<iostream>
 
using namespace std;
 
#define MMAX 10000
#define tMAX (60*24)

int main(void){
 
    int N, M;
    int r;
    int t, n, m1, s;
    int q;
    int ts, te, m2;
    int num,cn;
    int data[MMAX+1][tMAX+1];
 
    while (cin >> N >> M){
        if (N == 0 && M == 0) break;
        for (int i = 0; i <= MMAX; i++){
            for (int j = 0; j <= tMAX; j++){
                data[i][j] = 0;
            }
        }
        cin >> r;
		
        for (int i = 0; i < r; i++){
            cin >> t >> n >> m1 >> s;
            if (s == 0) data[m1][t]--;
            else data[m1][t]++;
        }
		
        for (int i = 1; i <= M; i++){
            num = 0;
            for (int j = 0; j <= tMAX; j++){
                num += data[i][j];
                data[i][j] = num;
            }
        }
		cin>>q;
        for (int i = 0; i < q; i++){
            cin >> ts >> te >> m2;
            cn = 0;
            for (int j = ts; j < te; j++){
                if (data[m2][j] >= 1) cn++;
            }
            cout << cn << endl;
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
typedef long long ll;

#define INF 1000000000
#define ST 540

int t[1000][1000];

int main(){
    int n,m;
    while(cin>>n>>m, n|m){
        fill(t[0],t[1000], 0);
        int r;
        cin>>r;
        rep(i,r){
            int ti,a,b,s;
            scanf("%d %d %d %d\n", &ti, &a, &b, &s);
            if(s==0) t[b-1][ti-ST] += -1;
            else t[b-1][ti-ST] += 1;
        }

        // imosu
        rep(i,1000){
            rep(j,800){
                t[i][j+1] += t[i][j];
            }
        }

        int q;
        cin>>q;
        rep(i,q){
            int s,e,a;
            scanf("%d%d%d",&s,&e,&a);
            s-=ST; e-=ST;
            int res=0;
            for(int j=s; j<e; j++){
                if(t[a-1][j]>0) res++;
            }
            cout<<res<<endl;
        }
    }
    return 0;
}