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 <iostream>
#include <vector>
#include <map>

using namespace std;
typedef pair<int, int> pii;

int main(){
	ios_base::sync_with_stdio(false);
	while(true){
		int N, M;
		cin >> N >> M;
		if(N == 0 && M == 0){ break; }
		map< int, vector<int> > logdata;
		map< pii, int> login_time;
		int r, q;
		cin >> r;
		while(r--){
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if(logdata.find(m) == logdata.end()){
				logdata.insert(make_pair(m, vector<int>(1000)));
			}
			vector<int> &entry = logdata[m];
			if(s == 0){
				for(int i = t - 540; i < entry.size(); ++i){ --entry[i]; }
			}else{
				for(int i = t - 540; i < entry.size(); ++i){ ++entry[i]; }
			}
		}
		cin >> q;
		while(q--){
			int ts, te, m, answer = 0;
			cin >> ts >> te >> m;
			if(logdata.find(m) == logdata.end()){
				cout << 0 << endl;
			}else{
				vector<int> &entry = logdata[m];
				for(int i = ts - 540; i < te - 540; ++i){
					answer += (entry[i] ? 1 : 0);
				}
				cout << answer << endl;
			}
		}
	}
	return 0;
}

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

bitset<720> bm[10001];
#define D map<pair<int, int>,int >
D d;

main(){
	int i,r,q,N,M,t,n,m,s,ts,te;
	while(cin>>N>>M,N){
		d.clear();
		cin>>r;
		for(i=1;i<=N;i++) bm[i].reset();
		while(r--){
			cin >> t >> n >> m >>s;
			t -= 540;
			if(s){
				d[make_pair(n,m)] = t;
			}else{
				ts = d[make_pair(n,m)];
				for(;ts<t;ts++) bm[m].set(ts);
			}
		}
		cin>>q;
		for(i=0;i<q;i++){
			cin >> ts >> te >> m;
			ts -= 540;
			te -= 540;
			for(s=0;ts<te;ts++) s+=bm[m][ts];
			cout << s << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int arr[10001][1262]={0};

int main(){
    int N, M;
    while(scanf("%d %d",&N,&M),N != 0 && M != 0){
        for(int i=0;i<10001;++i){
            for(int j=0;j<1262;++j){
                arr[i][j] = 0;
            }
        }
        int r;
        scanf("%d",&r);
        for(int i=0;i<r;++i){
            int t,n,m,s;
            scanf("%d %d %d %d",&t,&n,&m,&s);
            arr[m][t+1] = (s == 1 ? 1 : -1);
        }
        int q;
        scanf("%d",&q);
        for(int i=0;i<q;++i){
            int ts,te,m;
            scanf("%d %d %d",&ts,&te,&m);
            int acc = 0, tm = 0;
            for(int j=540;j<=1260;++j){
                acc += arr[m][j];
                if(j > ts && j <= te && acc > 0)tm++;
            }
            printf("%d\n",tm);
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) REP(i,0,n)
#define INF (1<<30)
#define INFLL (1LL<<62LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int N, M;
  while (cin >> N >> M, N && M) {
    int r, q;
    vector<vector<int>> record(M, vector<int>(1261, 0));

    cin >> r;
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      n--;
      m--;
      if (s) record[m][t]++;
      else record[m][t]--;
    }
    
    rep(i, M) rep(t, 1261)
        if(t) record[i][t] += record[i][t-1];

    cin >> q;
    rep(i, q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;

      int ans = 0;
      REP(t, ts, te) ans += (record[m][t] != 0);
      cout << ans << endl;
    }
  }
  return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>
#include <regex>

using namespace std;

int main() {

    while (true) {

        int N, M;
        cin >> N >> M;

        if (N == 0 && M == 0) break;

        int r;
        cin >> r;

        map<pair<int, int>, int> MAP;
        vector<vector<int>> students(M, vector<int>(1270, 0));

        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            m--;

            if (s == 1) {
                MAP[make_pair(n, m)] = t;
            }
            if (s == 0) {
                int start = MAP[make_pair(n, m)];
                int end = t;
                for (int j = start; j < end; j++) students[m][j] = 1;
            }
        }

        int Q;
        cin >> Q;

        for (int i = 0; i < Q; i++) {
            int ts, te, m;
            cin >> ts >> te >> m;
            m--;

            int ans = 0;
            for (int j = ts; j < te; j++) if (students[m][j])ans++;
            cout << ans << endl;
        }

//        for (int i = 0; i < M; i++) {
//            for (int j = 0; j < students[i].size(); j++) cout << students[i][j] << " ";
//            cout << endl;
//        }

    }

    return 0;
}