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 <iostream>
#include <algorithm>
using namespace std;
int main(){
    while(true){
        int N,M;
        cin>>N>>M;
        if(N==0)break;
        int r;
        cin>>r;
        vector<int> t(r),n(r),m(r),s(r);
        for(int i=0;i<r;i++){
            cin>>t[i]>>n[i]>>m[i]>>s[i];
        }
        int q;
        cin>>q;
        vector<int> ts(q),te(q),qm(q);
        for(int i=0;i<q;i++){
            cin>>ts[i]>>te[i]>>qm[i];
        }
        for(int i=0;i<q;i++){
            int sum=0;
            int start=0;
            int PCnum=0;
            for(int j=0;j<r;j++){
                if(m[j]==qm[i]){
                    if(s[j]==1){
                        PCnum++;
                        if(PCnum==1)
                            start=t[j];
                    }else{
                        PCnum--;
                        if(PCnum==0)
                            sum+=min(t[j],te[i])-max(ts[i],start);
                    }
                }
            }
            cout<<sum<<endl;
        }
    }
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 99999999



int main(){
    int n,m;
    while(cin>>n>>m&&n&&m){
        static int data[10001][1261-500];//500テ」ツつ津ィツカツウテ」ツ??
        memset(data,0,sizeof(data));
        int r;
        cin>>r;
        rep(i,r){
            int a,b,c,d;
            cin>>a>>b>>c>>d;
            if(d!=0){
                data[c][a-500]++;
            }else{
                data[c][a-500]--;
            }
        }
        rep(i,m+1){
            rep(j,1261-500-1){
                data[i][j+1]+=data[i][j];
//                cout<<data[i][j];
            }
//            cout<<endl;
        }
        int q;
        cin>>q;
        rep(i,q){
            int s,e,v;
            cin>>s>>e>>v;
            int sum=0;
            for(int j=s-500;j<e-500;j++)sum+=!!data[v][j];
            cout<<sum<<endl;
        }
    }
    
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

void solve(int n,int m){
	int r,q,t,in,im,is;
	vector<vector<pii>> s(m);
	cin>>r;
	rep(i,r){
		cin>>t>>in>>im>>is;
		in--;im--;
		s[im].push_back(pii(t,(is==1?1:-1)));
	}
	rep(i,m)sort(all(s[i]));
	cin>>q;
	int ts,te;
	while(q--){
		cin>>ts>>te>>im;
		im--;
		int cnt=0,time=0,ans=0;
		rep(i,s[im].size()){
			if(s[im][i].sc>0){
				cnt++;
				if(cnt==1){
					time=min(te,max(ts,s[im][i].fs));
				}
			}
			else{
				cnt--;
				if(cnt==0){
					ans+=min(te,max(ts,s[im][i].fs))-time;
				}
			}
		}
		cout<<ans<<endl;
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,m;
	while(cin>>n>>m,n)solve(n,m);
}

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

using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true){
    int n,m;cin >>n>>m;
    if(n==0) break;
    int r,q;
    int time[m+1][1300];
    for(int i=0;i<m+1;i++)for(int j=0;j<1300;j++)time[i][j]=0;
    cin>>r;
    for(int i=0;i<r;i++){
      int t,pc,stu,s;cin>>t>>pc>>stu>>s;
      if(s) time[stu][t]++;
      else time[stu][t]--;
    }
    for(int i=1;i<=m;i++)for(int j=1;j<1265;j++)time[i][j]+=time[i][j-1];
    cin>>q;
    for(int i=0;i<q;i++){
      int ts,te,stu;cin>>ts>>te>>stu;
      int res=0;
      for(int j=ts;j<te;j++)if(time[stu][j])res++;
      cout <<res<<endl;
    }
  }
}