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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k;
  int t[1000],o[1000],p[1000],s[1000],c[50];
  int m,n,r,q,b,e,l,u;
  int sz;
  vector<int> z;
  while(cin>>n>>m&&n+m){
    cin>>r;
    for(i=0;i<r;i++)
      cin>>t[i]>>o[i]>>p[i]>>s[i];
    cin>>q;
    for(i=0;i<q;i++){
      cin>>b>>e>>l;
      z.clear();
      u=0;
      for(j=0;j<r;j++){
	if(p[j]==l){
	  if(s[j]){
	    if(u==0)
	      z.push_back(t[j]);
	    u++;
	  }else{
	    u--;
	    if(u==0)
	      z.push_back(t[j]);
	  }
	}
      }
      sz=z.size();
      if(sz==0){
	c[i]=0;
      }else{
	if(b<z[0]){
	  j=-1;
	}else{
	  for(j=0;j<sz;j+=2){
	    if(z[j]<=b&&b<=z[j+1])
	      break;
	  }
	}
	if(e<z[0]){
	  k=-1;
	}else{
	  for(k=0;k<sz;k+=2){
	    if(z[k]<=e&&e<=z[k+1])
	      break;
	  }
	}
	if(0){
	}else if(j==sz||k==-1){
	  c[i]=0;
	}else if(j==k){
	  c[i]=e-b;
	}else if(j==-1&&k==sz){
	  c[i]=0;
	  for(j=0;j<sz;j+=2)
	    c[i]+=z[j+1]-z[j];
	}else if(j==-1){
	  c[i]=e-z[k];
	  for(j=0;j<k;j+=2)
	    c[i]+=z[j+1]-z[j];
	}else if(k==sz){
	  c[i]=z[j+1]-b;
	  for(j+=2;j<sz;j+=2)
	    c[i]+=z[j+1]-z[j];
	}else{
	  c[i]=z[j+1]-b;
	  c[i]+=e-z[k];
	  for(j+=2;j<k;j+=2)
	    c[i]+=z[j+1]-z[j];
	}
      }
    }
    for(i=0;i<q;i++)
      cout<<c[i]<<endl;
  }
  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,cnt;
    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]++;
        }
		return 0;
        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;
            cnt = 0;
            for (int j = ts; j < te; j++){
                if (data[m2][j] >= 1) cnt++;
            }
            cout << cnt << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define fordebug int hoge;cin>>hoge;
int a[10000][2000];
int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0&&m==0) break;
    int r;
    cin>>r;
    lp(i,r){
      int t,nn,mm,s;
      cin>>t>>nn>>mm>>s;
      if(s==0) a[mm][t+1]=-1; 
      else a[mm][t]=1;
    }
    lp(i,m+1){
      for(int j=1;j<2000;j++){
	a[i][j]+=a[i][j-1];
      }
    }
    int q;
    cin>>q;
    lp(i,q){
      int x,y,z;
      cin>>x>>y>>z;
      int count=0;
      for(int j=x;j<=y;j++){
	if(a[z][j-1]==0&&a[z][j]==1&&j!=x) count--;
	if(a[z][j]!=0) count++;
	if(j==x&&a[z][j]==1) count--;
      }
      cout<<count<<endl;
    }
    lp(i,m+1){
      lp(j,2000){
	a[i][j]=0;
      }
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF (1LL << 25) // 33554432
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

int pc[10010][1300];

signed main(void) {
  while (true) {
    int n, m, r, q;
    cin >> n >> m;
    if(!n && !m) break;
    REP(i, m) FOR(j, 540, 1300) pc[i][j] = 0;
    cin >> r;
    REP(i, r) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      if (d)
        pc[c][a]++;
      else
        pc[c][a+1]--;
    }
    REP(i, m+1) FOR(j, 541, 1300) {
      pc[i][j] += pc[i][j - 1];
    }

    cin >> q;
    REP(i, q) {
      int a, b, c;
      cin >> a >> b >> c;
      int s = 0, ret = 0;
      bool flag = false;
      FOR(j, a, b+1) {
        //cout << pc[c][j] << " ";
        if (!flag && pc[c][j]) {
          s = j;
          flag = true;
        } else if(flag && !pc[c][j]) {
          ret += (j-s-1);
          flag = false;
        }
        if(j == b && pc[c][j]) {
          ret += (j-s);
        }
        //cout << s << " " << j << endl;
      }
      cout << ret << endl;
    }
  }

  return 0;
}