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<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){
    int ans[m][2000]={};
    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;
      for(int j=0;j<=2000;j++){

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

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <numeric>

#define rep(i, j) for(int i = 0; i < j; i++)
#define all(i) i.begin(), i.end()

using namespace std;

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M; while (cin >> N >> M && N != 0 && M != 0)
	{
		int r; cin >> r;
		vector<vector<int>> dat(M + 2, vector<int>(722, 0));
		vector<vector<int>> ttmp(M + 2, vector<int>(N + 2, 0));
		rep(i, r)
		{
			int t, n, m, s; cin >> t >> n >> m >> s;
			if (s == 1){ ttmp[m][n] = t - 540; }
			else if (s == 0) { fill(dat[m].begin() + ttmp[m][n], dat[m].begin() + (t - 540), 1); }
		}
		int q; cin >> q;
		rep(i, q)
		{
			int ts, te, m; cin >> ts >> te >> m;
			printf("%d\n", accumulate(dat[m].begin() + (ts - 540), dat[m].begin() + (te - 540), 0));
		}
	}
	return 0;
}

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

int solve(vector<multimap<int,bool> > &record,int ts,int te,int a){
  int d=0;int sum=0,start=0;
  for(multimap<int,bool>::iterator it=record[a-1].begin();it!=record[a-1].end();++it){
    if(!d&&it->second)start=it->first;
    if(it->second)++d;
    if(!it->second)--d;
    if(!d){
      int st,en;
      st=(ts>start)?ts:start;
      en=(te<it->first)?te:it->first;
      sum+=en-st;
    }
  }
  return sum;
}

int main(void){
  int n,m;
  while(cin>>n>>m,n||m){
    int r,q;
    cin>>r;
    vector<multimap<int,bool> > record(m);
    for(int i=0,t,a,b,s;i<r;++i){
      cin>>t>>a>>b>>s;
      record[b-1].insert(make_pair(t,((s)?true:false)));
    }
    cin>>q;
    for(int i=0,ts,te,a;i<q;++i){
      cin>>ts>>te>>a;
      cout<<solve(record,ts,te,a)<<endl;
    }
  }
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int use[10004][1300]; 
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  while(cin>>N>>M,N||M) {
    CLR(use);
    int r; cin >> r;
    FOR(i,0,r) {
      int t, n , m , s;
      cin >> t >> n >> m >> s;
      if(s == 1) {
        use[m][t]++;
      } else {
        use[m][t]--;
      }
    }
    FOR(i,1,M+1) {
      FOR(j,0,1299) {
        use[i][j+1] += use[i][j];
      }
    }
    int q; cin >> q;
    FOR(i,0,q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int ans = 0;
      FOR(j,ts,te) {
        if(use[m][j] > 0) ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=true;
/*---------------------------------------------------*/

int imos[1265];
//plog[????????????][PC??????]
int plog[1005][10005];

void init(){
  rep(i,1265)imos[i]=0;
}

int main(){
  int N,M;
  int r,q;
  int t,n,m,s;
  while(cin>>N>>M){
    if(N==0&&M==0)break;
    vector<pi> data[M+1];
    cin>>r;
    rep(i,r){
      //input data
      cin>>t>>n>>m>>s;
      if(s){
	plog[n][m]=t;
      }else{
	data[m].pb(mp(plog[n][m],t));
	plog[n][m]=0;
      }
    }
    //question
    cin>>q;
    rep(i,q){
      int ts,te;
      init();
      cin>>ts>>te>>m;
      rep(j,data[m].size()){
	imos[data[m][j].first]+=1;
	imos[data[m][j].second+1]-=1;
      }
      REP(j,1,1265)imos[j]+=imos[j-1];
      int ans=0;
      int cnt=0;
      REP(j,ts,te+1){
	if(imos[j])cnt++;
	else {ans+=max(cnt-1,0);cnt=0;}
      }
      ans+=max(cnt-1,0);
      cout<<ans<<endl;
    }
  }
}