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 <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
        int N,M; cin >> N >> M;
        if(N == 0 && M == 0) break;

        VI imos[M];
        VI exis[M];
        VI acc[M];
        REP(i,M) imos[i] = VI(1000,0);
        REP(i,M) exis[i] = VI(1000,0);
        REP(i,M) acc[i] = VI(1000,0);

        int r; cin >> r;
        REP(i,r){
            int t,n,m,s; cin >> t >> n >> m >> s;
            t -= 540;
            m--;
            if(s == 1){
                imos[m][t]++;
            }
            else{
                imos[m][t]--;
            }
        }

        REP(i,M){
            REP(j,1000){
                exis[i][j+1] = exis[i][j] + imos[i][j];
            }
            REP(j,1000){
                acc[i][j+1] = acc[i][j] + (exis[i][j] > 0 ? 1 : 0);
            }
        }
        int q; cin >> q;
        REP(i,q){
            int s,e,m; cin >> s >> e >> m;
            s -= 540; e-= 540;
            m--;
            cout << acc[m][e+1] - acc[m][s+1] << endl;
        }

        //REP(j,400){
            //cout << j+540 << " " << acc[0][j] << endl;
        //}
    }

}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX_N 1000
#define MAX_M 10000
#define MIN_T 540
#define MAX_T 1260

int main() {
	static int a[MAX_M][MAX_T + 1];
	for (int N, M; cin >> N >> M&&N;) {
		memset(a, 0, sizeof(a));

		int r; cin >> r;
		for (int i = 0; i < r; i++) {
			int t, n, m, s; cin >> t >> n >> m >> s;
			if (s == 1) {
				for (int j = t; j <= MAX_T; j++) {
					a[m][j]++;
				}
			}
			else {
				for (int j = t; j <= MAX_T; j++) {
					a[m][j]--;
				}
			}
		}
		int q; cin >> q;
		for (int i = 0; i < q; i++) {
			int ts, te, m; cin >> ts >> te >> m;
			int cnt = 0;
			for (int j = ts; j < te; j++) {
				//cout << j << " " << a[m][j] << endl;
				if (a[m][j] > 0)cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}

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

int main(){
  int N,M;
  while(cin>>N>>M){
    if(N==0)break;
    int r,q;
    int student[M][721];

    for(int i=0;i<M;i++)
      for(int j=0;j<721;j++)
	student[i][j]=0;
  
    cin>>r;
    for(int i=0;i<r;i++){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      t-=540;m--;
      if(s==1){
	student[m][t]=n;
      }else{
	while(student[m][--t]!=n){
	  if(student[m][t]==0) student[m][t]=n;
	}
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      int ts,te,m;
      cin>>ts>>te>>m;
      ts-=540;te-=540;m--;
      int ans=0;
      for(int i=ts;i<te;i++){
	if(student[m][i]>0)ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}