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"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M;
	while (cin >> N >> M, N&&M) {
		int r; cin >> r;
		vector<vector<int>>v(M, vector<int>(1300, 0));
		rep(i, 0, r) {
			int t, n, m, s; cin >> t >> n >> m >> s;
			m--;
			if (s) {
				v[m][t]++;
			}
			else {
				v[m][t]--;
			}
		}
		rep(i, 0, M) {
			rep(j, 1, v[i].size())v[i][j] += v[i][j - 1];
			rep(j, 0, v[i].size())v[i][j] = v[i][j] > 0;
			rep(j, 1, v[i].size())v[i][j] += v[i][j - 1];
		}

		int q; cin >> q;
		rep(i, 0, q) {
			int ts, te, m; cin >> ts >> te >> m;
			m--;
			//rep(i, ts-1, te + 1)cout << v[m][i] << (i == te + 9 ? '\n':  ' ');
			cout << v[m][te-1] - v[m][ts-1] << endl;

		}
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(){

	int N,M;
	int r;

	while(cin>>N>>M){
		if(N==0&&M==0)break;

		cin>>r;

		int *t = new int[r];
		int *n = new int[r];
		int *m = new int[r];
		int *s = new int[r];

		for(int i=0;i<r;i++){
			cin>>t[i]>>n[i]>>m[i]>>s[i];
		}

		int q;
		cin>>q;

		for(int i=0;i<q;i++){
			int start,end,studentNum;
			cin>>start>>end>>studentNum;

			int pcNum=0;
			int pcStart=0;
			int sum=0;

			for(int j=0;j<r;j++){

				if(studentNum == m[j]){
					if(s[j]==1){
						if(pcNum==0){
							if(t[j]<=start){
								pcStart=start;
							}
							else if(t[j]>=end){
								pcStart=end;
							}
							else{
								pcStart=t[j];
							}
						}
						pcNum++;
					}
					else{
						if(pcNum==1){
							if(t[j]<=start){
								sum += 0;
							}
							else if(t[j]>=end){
								sum += end -pcStart;
							}
							else{
								sum += t[j]-pcStart;
							}
						}
						pcNum--;
					}
				}
			}
			cout<<sum<<endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
using namespace std;

typedef pair <int, int> PII;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
bool U[2000];
int L[1001];

int main( void )
{
    int N, M;
    while ( cin >> N >> M && N && M ) {
        VVPII T( 10001 );
        memset( L, -1, sizeof(L) );
        int r, q;
        int t, n, m, s, ts, te;
        cin >> r;
        for ( int i = 0; i < r; i++ ) {
            cin >> t >> n >> m >> s;
            if ( s ) {
                L[n] = t;
            } else {
                T[m].push_back( PII( L[n], t ) );
                L[n] = -1;
            }
        }
        cin >> q;
        for ( int i = 0; i < q; i++ ) {
            memset( U, false, sizeof(U) );
            int answer = 0;
            cin >> ts >> te >> m;
            for ( VPII ::iterator it = T[m].begin(); it != T[m].end(); it++ ) {
                PII r = *it;
                if ( r.second < ts || r.first > te ) continue;
                for ( int j = r.first; j < r.second; j++ ) U[j] = true;
            }
            for ( int j = ts; j < te; j++ ) if ( U[j] ) answer++;
            cout << answer << endl;
        }
    }
    return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int check_start(std::vector<int> t, int ts) {
	for (int i = t.size()-1; i > 0; i--) {
		if (t[i] <= ts) {
			return i;
		}
	}
	return 0;
}

int check_end(std::vector<int> t, int te) {
	for (int i = 0; i < t.size(); i++) {
		if (t[i] >= te) {
			return i;
		}
	}
	return t.size()-1;
}

int main(void) {
	int N,M;
	cin >> N >> M;

	while( N != 0 || M != 0 ) {
		int r,q;
		int t,n,m,s;
		cin >> r;

		std::vector< std::stack<int> > l(M);
		std::vector<int> time(r);
		std::vector< std::vector<bool> > use(r, std::vector<bool>(M,false));
		for (int i = 0; i < r; i++) {
			cin >> time[i] >> n >> m >> s;
			if (s == 0) {
				l[m-1].pop();
			}
			else {
				l[m-1].push(n);
			}

			for (int j = 0; j < M; j++) {
				if (!(l[j].empty())) {
					use[i][j] = true;
				}
			}

		}

		cin >> q;
		int ts,te;
		int si,ei;
		for (int i = 0; i < q; i++) {
			cin >> ts >> te >> m;
			si = check_start(time,ts);
			ei = check_end(time,te);
			int sdt = ts - time[si];
			int edt = time[ei] - te;
			int ret = 0;
			int st = 0;
			bool start = false;
			bool end = false;
			bool freq = false;
			for (int j = si; j < ei+1; j++) {
				if (use[j][m-1]) {
					if (j == si) {
						start = true;
					}
					if (j == ei-1) {
						end = true;
					}
					if (!freq) {
						st = time[j];
					}
					freq = true;
				}
				if (!use[j][m-1] || j == ei) {
					if (freq) {
						ret += time[j] - st;
						freq = false;
					}
				}
			}
			if (start && sdt > 0) {
				ret -= sdt;
			}
			if (end && edt > 0) {
				ret -= edt;
			}
			cout << ret << endl;
		}
		cin >> N >> M;
	}

	return 0;
}