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++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
// #define USE_LLONG_AS_INT
#ifdef USE_LLONG_AS_INT
#define int long long
#define inf (1ll<<60)
#else
#define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=(n)-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

int a[10000][2000];

void run(){
    int n,m;
    cin>>n>>m;
    if(!n)return;
    int r;
    cin>>r;
    MEMSET(a);
    rep(_,r){
        int t,x,y,s;
        cin>>t>>x>>y>>s;
        y--;
        if(s)a[y][t]++;
        else a[y][t]--;
    }
    rep(i,m){
        rep(j,1999){
            a[i][j+1]+=a[i][j];
        }
    }
    int q;
    cin>>q;
    rep(_,q){
        int s,e,y;
        cin>>s>>e>>y;
        y--;
        int ans=0;
        Rep(i,s,e){
            ans+=a[y][i]!=0;
        }
        cout<<ans<<endl;
    }
    run();
}

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

#define MAX_M 10000+1
#define MAX_N 1000

int N, M;
int r, q;

vector< pair<int, int> > ts[MAX_M];
int cnt[MAX_M];
int from[MAX_M];
int main() {
    while (cin >> N >> M) {
        if (N == 0 && M == 0) break;
        for (int i = 0; i < MAX_M; i++) ts[i].clear();
        memset(cnt, 0, sizeof(cnt));
        memset(from, 0, sizeof(from));
        cin >> r;
        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if (s == 1) {
                if (cnt[m] == 0) from[m] = t;
                cnt[m]++;
            } else {
                cnt[m]--;
                if (cnt[m] == 0) 
                    ts[m].push_back(make_pair(from[m], t));
            }
        }
        cin >> q;
        for (int i = 0; i < q; i++) {
            int s, e, m;
            cin >> s >> e >> m;
            int total = 0;
            for (int j = 0; j < ts[m].size(); j++) {
                if (s < ts[m][j].first) {
                    if (e < ts[m][j].first) continue;
                    if (e <= ts[m][j].second) {
                        total += (e - ts[m][j].first);
                    } else {
                        total += (ts[m][j].second - ts[m][j].first);
                    }
                } else {
                    if (s < ts[m][j].second) {
                        if (e <= ts[m][j].second) {
                            total += (e - s);
                        } else {
                            total += (ts[m][j].second - s);
                        }
                    }
                }
                //cout << ts[m][j].first << ' ' << ts[m][j].second << endl;
            }
            cout << total << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	int nn,mm;
	while(cin>>nn>>mm){
		if(nn==0&&mm==0) break;
		int r;
		cin>>r;
		int a[r][4];
		for(int i=0;i<r;i++){
			for(int j=0;j<4;j++){
				cin>>a[i][j];
			}
		}
		int q;
		cin>>q;
		for(int i=0;i<q;i++){
			int ts,te,m,to=0,ct=0;
			cin>>ts>>te>>m;
			for(int j=0;j<r;j++){
				if(a[j][2]==m){
					if(a[j][0]<=te){
						if(ct==0){
							if(a[j][0]<ts) to=to-ts;
							else to=to-a[j][0];
						}
						if(a[j][3]==1) ct++;
						else ct--;
						if(ct==0){
							if(a[j][0]<ts) to=to+ts;
							else to=to+a[j][0];
						}
					}
					else{
						if(ct>0) to=to+te;
						goto end;
					}
				}
			}
			end:;
			cout<<to<<endl;
		}
	}
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

struct range{ int start, end, pc;};

int main()
{
    int N, M;
    while(cin >> N >> M && N && M) {
        vector< vector<range> > students(M + 1);
        int r, q;
        cin >> r;
        for(int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if(s == 1) {
                students[m].push_back((range){t, -1, n});
            } else {
                for(int j = 0; j < students[m].size(); j++) {
                    if(students[m][j].end == -1 && students[m][j].pc == n) {
                        students[m][j].end = t;
                    }
                }
            }
        }
        cin >> q;
        for(int i = 0; i < q; i++) {
            int ts, te, m, sum;
            cin >> ts >> te >> m;
            sum = 0;
            for(int t = ts + 1; t <= te; t++) {
                for(int j = 0; j < students[m].size(); j++) {
                    if(students[m][j].start < t && students[m][j].end >= t) {
                        sum++;
                        break;
                    }
                }
            }
            cout << sum << endl;
        }
    }
}