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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,c) memset(a,c,sizeof(a))

#define DEBUG(x) cout<<"#x"<<": "<<x<<endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX/3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};

int main(){
    while(true){
	int N,M;
	int r,q;
	cin >> N >> M;
	if(N == 0 && M == 0) break;
	int t[1001],n[1001],m[1001],s[1001];
	int ts[51],te[51],tm[51];
	cin >> r;
	REP(i,r){
	    cin >> t[i] >> n[i] >> m[i] >> s[i];
	}
	cin >> q;
	REP(i,q){
	    cin >> ts[i] >> te[i] >> tm[i];
	    int time = ts[i];
	    int c = 0;
	    int ans = 0;
	    for(int j=0; t[j] <= te[i] && j<r;j++){
		if(tm[i] == m[j]){
		    if(t[j] >= ts[i] && c > 0){
			ans += t[j] - time;
// 			printf("s = %d e = %d\n",time,t[j]);
		    }
		    c += s[j] == 1 ? 1 : -1;
		    time = max(ts[i],t[j]);
		}
	    }
	    if(c > 0) ans += te[i]-time;
	    cout << ans << endl;
	}
    }

    return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int main(){
	int n, m;
	while(cin >> n >> m, n){
		cin >> n;
		vector <int> count(m, 0);
		vector < vector< pair<int,int> > > rec(m);
		int t, p, s;
		for(int i=0;i<n;i++){
			cin >> t >> p >> p >> s;
			p--;
			if(s == 1){
				if(count[p] == 0)
					rec[p].push_back(make_pair(t,t));
				count[p]++;
			} else {
				count[p]--;
				if(count[p] == 0)
					rec[p].back().second = t;
			}
		}
		cin >> n;
		for(int i=0;i<n;i++){
			int ans = 0;
			cin >> s >> t >> p;
			p--;
			for(int j=0;j<rec[p].size();j++){
				ans += max(0, min(t, rec[p][j].second) - max(s, rec[p][j].first));
			}
			cout << ans << endl;
		}
	}
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
 
using namespace std;

int main()
{
    int N, M;
    while(1){
        cin >> N >> M;
        if(N == 0 && M == 0) break;
        int r;
        int records[1000][4];
        cin >> r;
        for(int i = 0 ;i < r; i++){
            for(int j = 0; j < 4; j++){
                cin >> records[i][j];
            }
        }
        // record[i][0]が時刻、1がPCnum,2がstudentnum, 3がlogin/logout
        int q, t_s, t_e, m;
        bool isoverlap = false;
        cin >> q;
        for(int i = 0; i < q; i++){
            cin >> t_s >> t_e >> m;
            int login_pc = 0, t_sum = 0, t_loginfirst = t_s, t_logoutlast = t_e;
            for(int j = 0; j < 1000; j++){
                if(records[j][2] == m){
                    if(records[j][0] >= t_s || records[j][0] <= t_e) isoverlap = true;
                    if(records[j][3] == 1){
                        if(login_pc == 0) t_loginfirst = max(records[j][0], t_s);
                        // cout << "t-s" << t_loginfirst << endl;
                        login_pc++;
                    } else if(records[j][3] == 0){
                        login_pc--;
                        if(login_pc == 0){
                            t_logoutlast = min(records[j][0], t_e);
                            // cout << "t-l" << t_logoutlast << endl;
                            if(isoverlap) {
                                // cout << t_loginfirst << ' ' << t_logoutlast << endl;
                                t_sum += (t_logoutlast - t_loginfirst);
                            }
                            if(records[j][0] > t_e) break;
                        }
                    }
                }
            }
            cout << t_sum << endl;
        }
    }
    return 0;
}