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>
#define NUMBER 10000
#define TIME 1261
using namespace std;

int record[NUMBER][TIME];
int rec[1000][4];
int quest[50][3];

void answer_question(int q) {
  for (int i = 0; i < q; i++) {
    int student = quest[i][2];
    int count = 0;
    for (int j = quest[i][0]; j <= quest[i][1]; j++) {
      int t = -1;
      if (record[student][j]) {
	for (int k = j; k <= quest[i][1]; k++) {
	  if (record[student][k] == 0) {
	    t = k - 1;
	    break;
	  }
	}
	if (t == -1) t = quest[i][1];
	count += t - j;
	j = t;
      }
    }
    cout << count << endl;
  }
}

void record_search(int r) {
  for (int i = 0; i < r; i++) {
    if (rec[i][3] == 0) continue;
    for (int j = i + 1; j < r; j++) {
      if (rec[j][1] == rec[i][1] && rec[j][2] == rec[i][2] && rec[j][3] == 0) {
	for (int k = rec[i][0]; k <= rec[j][0]; k++) {
	  record[rec[i][2]][k] = 1;
	}
	goto label;
      }
    }
  label: ;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;

    int r, q;
    cin >> r;
    for (int i = 0; i < r; i++)
      cin >> rec[i][0] >> rec[i][1] >> rec[i][2] >> rec[i][3];

    cin >> q;
    for (int i = 0; i < q; i++)
      cin >> quest[i][0] >> quest[i][1] >> quest[i][2];

    for (int i = 0; i < r; i++)
      for (int j = 0; j < TIME; j++)
	record[i][j] = 0;
    record_search(r);
    answer_question(q);
  }
}

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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX/3

#define MAX_N 1000

int use[1111][1300];
int N,M,r,q;

int main(){
	cin.sync_with_stdio(false);
	while(1){
		cin>>N>>M;
		if(N==0)break;
		memset(use,0,sizeof(use));
		cin>>r;
		rep(i,r){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			n--; m--;
			if(s==1){
				use[m][t]+=1;
			}else{
				use[m][t]-=1;
			}
		}
		rep(i,M)repl(j,540,1300){
			use[i][j+1]+=use[i][j];
		}
		cin>>q;
		rep(i,q){
			int ts,te,m;
			cin>>ts>>te>>m;
			m--;
			int res=0;
			repl(j,ts,te){
				if(use[m][j]>0)res++;
			}
			cout<<res<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m,n){
    short ans[m][1261]={};
    int r;cin>>r;
    for(int i=0;i<r;i++){
      int t,nn,mm,s;cin>>t>>nn>>mm>>s;
      mm--;
      if(s==1)
	ans[mm][t]=1;
      else{
	ans[mm][t]=-1;
      }
    }
    int q;cin>>q;
    for(int i=0;i<q;i++){
      int t,tt,mm;cin>>t>>tt>>mm;mm--;
      int ans2=0;
	int cnt=0;
	int cnt2=0;
      for(int j=0;j<=2000;j++){

	cnt+=ans[mm][j];
	//cout<<cnt<<endl;;
	if(cnt>0&&j<=tt&&t<=j){cnt2++;}
	else{
	  ans2+=cnt2-1;
	}
      }
      cout<<ans2<<endl;
    }
  }
  return 0;
}

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

typedef pair <int, int> PII;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
bool U[700];
int L[1001];

int main( void )
{
    int N, M;
    while ( cin >> N >> M && N && M ) {
        VVPII T( M );
        memset( L, -1, sizeof(L) );
        int r, q;
        int t, n, m, s, ts, te;
        cin >> r;
        for ( int i = 0; i < r; i++ ) {
            cin >> t >> n >> m >> s;
            t -= 540;
            if ( s ) {
                L[n] = t;
            } else {
                T[m].push_back( PII( L[n], t ) );
                L[n] = -1;
            }
        }
        cin >> q;
        for ( int i = 0; i < q; i++ ) {
            memset( U, false, sizeof(U) );
            int answer = 0;
            cin >> ts >> te >> m;
            ts -= 540;
            te -= 540;
            for ( VPII ::iterator it = T[m].begin(); it != T[m].end(); it++ ) {
                PII r = *it;
                if ( r.second < ts || r.first > te ) continue;
                for ( int j = r.first; j < r.second; j++ ) U[j] = true;
            }
            for ( int j = ts; j < te; j++ ) if ( U[j] ) answer++;
            cout << answer << endl;
        }
    }
    return 0;
}