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 r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[p][1261-539],ch[p][1261-539],b,c,d,e;
		r(i,0,p)r(j,0,1261-539)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			b-=539;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			b-=539;
			c-=539;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>

typedef long long ll;
using namespace std;



struct Slog{
	int pc;
	int time;
	int inout;
	
	Slog(){}
	Slog(int n, int t, int s) : pc(n), time(t), inout(s) {}
};

struct Slog2{
	int in;
	int out;
	
	bool operator< (const Slog2 &s) const{
		return (*this).in < s.in;
	}
};

struct Sstu{
	int num;
	vector<Slog> log;
	vector<Slog2> log2;
	
	Sstu(){}
	void MakeLog2(int N){
		vector<Slog2> list(N);
		for(int i=0;i<log.size();i++){
			if(log[i].inout == 1){
				list[log[i].pc-1].in = log[i].time;
				list[log[i].pc-1].out = log[i].time;
			}
			if(log[i].inout == 0){
				list[log[i].pc-1].out = log[i].time;
				log2.push_back(list[log[i].pc-1]);
			}
		}
		sort(log2.begin(), log2.end());
	}
	void print(){
		cout<< " student_"<< num<< endl;
		for(int i=0;i<log.size();i++){
			cout<< " "<< log[i].pc<< " "<< log[i].time<< " "<< log[i].inout << endl;
		}
	}
	void print2(){
		if(!log2.size()) return;
		cout<< " student_"<< num<< endl;
		for(int i=0;i<log2.size();i++){
			cout<< " "<< log2[i].in<< " "<< log2[i].out<< endl;
		}
	}

};

bool solve(){
	int N, M, r, q;
	cin>> N>> M;
	if(N==0 && M==0) return false;
	
	vector<Sstu> stu(M);
	for(int i=0;i<M;i++) stu[i].num = i+1;
	cin>> r;
	for(int i=0;i<r;i++){
		int t, n, m, s;
		cin>> t>> n>> m>> s;
		Slog in(n,t,s);
		stu[m-1].log.push_back(in);
	}
//	for(int i=0;i<M;i++) stu[i].print();
	for(int i=0;i<M;i++) stu[i].MakeLog2(N);
//	for(int i=0;i<M;i++) stu[i].print2();
	
	cin>> q;
	for(int i=0;i<q;i++){
		int ans = 0;
		int ts, te, num;
		cin>> ts>> te>> num;
//		cout<< "  "<< ts<<" "<< te<<" "<< num<< endl;
		Sstu *s = &stu[num-1];
		int end = ts;
		for(int j=0; j < s->log2.size(); j++){
			if(s->log2[j].out < end || s->log2[j].in > te) continue;
			ans += min(s->log2[j].out, te) - max(s->log2[j].in, end);
			end = s->log2[j].out;
		}
		cout<< ans<< endl;
	}
	
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	for (;;) {
		int N, M; cin >> N >> M;
		if (N == 0) break;
		int r; cin >> r;
		vector<int> t(r), n(r), m(r), s(r);
		for (int i = 0; i < r; i++)
			cin >> t[i] >> n[i] >> m[i] >> s[i];
		int q; cin >> q;
		while (q--) {
			int ts, te, _m; cin >> ts >> te >> _m;
			int x = 0, start, end, ans = 0;
			for (int i = 0; i < r; i++) {
				if (m[i] != _m) continue;
				if (s[i]) {
					if (x == 0) start = t[i];
					x++;
				}
				else {
					x--;
					if (x == 0) {
						end = t[i];
						ans += max(0, min(te, end) - max(ts, start));
					}
				}
			}
			cout << ans << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

pint ss[10001];
vector<pint> p[10001];
int c[10001];
int main(){
    int n,m,r,q;
    while(cin>>n>>m,n){
        rep(i,m) p[i].clear();
        cin>>r;
        int t,pc,st,s;
        rep(i,r){
            cin>>t>>pc>>st>>s;
            if(s==1){
                ++c[st];
                if(c[st]==1) ss[st].first=t;
            }
            else{
                --c[st];
                if(c[st]==0) ss[st].second=t,p[st].pb(ss[st]);
            }
        }
        cin>>q;
        rep(i,q){
            int ts,te,tm;
            cin>>ts>>te>>tm;
            int ans=0;
            for(auto it:p[tm]){
                ans+=max(0,min(it.second,te)-max(it.first,ts));
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}