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;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1, 0 };
#define all(u) begin(u),end(u)
#define mp make_pair
#define pb push_back


int main()
{
  while(true)
    {
      int N, M;
      scanf("%d%d", &N,&M);
      if(N == 0 && M == 0)break;
      int A[M+1][1500]={0};
      int r;
      scanf("%d", &r);
      rep(i, r)
	{
	  int t, n, m, s;
	  scanf("%d%d%d%d", &t, &n, &m, &s);
	  if(s == 1)
	    {
	      A[m][t]++;
	    }
	  else
	    {
	      A[m][t+1]--;
	    }
	}
      repi(i, 1, M + 1)
	{
	  rep(t, 1300)
	    {
	      if(t == 0)continue;
	      A[i][t] += A[i][t-1];
	    }
	}

      int q;
      scanf("%d", &q);
      rep(i, q)
	{
	  int ts, te, m;
	  scanf("%d%d%d", &ts,&te,&m);
	  int start = -1;
	  int ans = 0;
	  repi(t, ts, te+1)
	    {
	      if(start == -1)
		{
		  if(A[m][t] > 0)
		    {
		      start = t;
		    }
		}
	      else
		{
		  if(A[m][t] <= 0)
		    {
		      ans += t - start - 1;
		      start = -1;
		    }
		}
	    }
	  if(start != -1)ans += te - start;
	  printf("%d\n", ans);
	}
    }
  return 0;
}

C++: #include<iostream>
 
using namespace std;
 
#define MMAX 10000
#define tMAX (60*24)

int main(void){
 
    int N, M;
    int r;
    int t, n, m1, s;
    int q;
    int ts, te, m2;
    int num,cn;
    int data[MMAX+1][tMAX+1];
 
    while (cin >> N >> M){
        if (N == 0 && M == 0) break;
        for (int i = 0; i <= MMAX; i++){
            for (int j = 0; j <= tMAX; j++){
                data[i][j] = 0;
            }
        }
        cin >> r;
		
        for (int i = 0; i < r; i++){
            cin >> t >> n >> m1 >> s;
            if (s == 0) data[m1][t]--;
            else data[m1][t]++;
        }
		
        for (int i = 1; i <= M; i++){
            num = 0;
            for (int j = 0; j <= tMAX; j++){
                num += data[i][j];
                data[i][j] = num;
            }
        }
		cin>>q;
        for (int i = 0; i < q; i++){
            cin >> ts >> te >> m2;
            cn = 0;
            for (int j = ts; j < te; j++){
                if (data[m2][j] >= 1) cn++;
            }
        }
    }
    return 0;
}

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

int N, M, r, q;
int t[1024], n[1024], m[1024], s[1024];

vector< vector<pair<int,int> > >  log(10010); // v[id][num] = time
vector<pair<pair<int, int>, int> > query;

bool input() {
	cin >> N >> M;
	if(N == 0 && M == 0)
		return false;
	cin >> r;
	for(int i = 0; i < r; i++) {
		cin >> t[i] >> n[i] >> m[i] >> s[i];
	}
	cin >> q;
	for(int i = 0; i < q; i++) {
		int tmp_ts, tmp_te, tmp_m;
		cin >> tmp_ts >> tmp_te >> tmp_m;
		query.push_back( pair<pair<int,int> ,int>(pair<int,int>(tmp_ts, tmp_te), tmp_m) );
	}

	return true;
}

void init() {
	log.clear();
	query.clear();
}

void mklog() {
	log.resize(M);
	for(int i = 0; i < r; i++) {
		if(s[i] == 1) {
			if(log[m[i] - 1].size() && log[m[i] - 1][log[m[i] - 1].size() - 1].second == -1)
				break;
			log[m[i] - 1].push_back(pair<int, int>(t[i], -1));
		}
		else
			log[m[i] - 1][log[m[i] - 1].size() - 1].second = t[i];
	}
}

int solve(pair<pair<int,int>, int>qnum) {
	int ans = 0;
	for(int i = 0; i < log[qnum.second - 1].size(); i++) {
		if(log[qnum.second - 1][i].second > qnum.first.first && log[qnum.second - 1][i].first < qnum.first.second) {
			int start, end;
			if(log[qnum.second - 1][i].first > qnum.first.first)
				start = log[qnum.second - 1][i].first;
			else
				start = qnum.first.first;

			if(log[qnum.second - 1][i].second < qnum.first.second)
				end = log[qnum.second - 1][i].second;
			else
				end = qnum.first.second;
			
			ans += end - start;
		}
	}
	return ans;
}

int main(void) {
	while(init(), input()) {
		mklog();
		for(int i = 0; i < q; i++) {
			cout << solve(query[i]) << endl;
		}
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

#define MMAX 100
#define tMAX 12600
int main(){

	int N, M;
	int r;
	int t, n, m1, s;
	int q;
	int ts, te, m2;
	int num,cnt;
	int data[MMAX+1][tMAX+1];
return 0;
	while (1){
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j <= tMAX; j++){
				data[i][j] = 0;
			}
		}
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m1 >> s;
			if (s == 0) data[m1][t]=-1;
			else data[m1][t]=1;
		}
		cin >> q;
		for (int i = 1; i <= M; i++){
			num = 0;
			for (int j = 0; j <= tMAX; j++){
				num += data[i][j];
				data[i][j] = num;
			}
		}
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m2;
			cnt = 0;
			for (int j = ts; j < te; j++){
				if (data[m2][j] >= 1) cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}