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<vector>
#include<algorithm>

#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;
int student[10001][720];

void init(){
    REP(i,10000){
        REP(j,720){
            student[i][j] = 0;
        }
    }
    return;
}

int main(){
    int N,M,r,q;
    while(cin >> N >> M){
        if(N+M==0) break;
        init();
        cin >> r;
        REP(i,r){
            int t,n,m,s;
            cin >> t >> n >> m >> s;
            m -= 1; //0base
            if(s==1) student[m][t-540]++;
            else if(s==0) student[m][t-540]--;
        }

        //simulate
        REP(i,M){
            REP(j,720){
                if(j>0) student[i][j] += student[i][j-1];
            }
        }
        REP(i,M){
            REP(j,720){
                if(student[i][j]>0) student[i][j] = 1;
            }
        }

        cin >> q;
        REP(i,q){
            int cnt = 0;
            int t_s, t_e, m;
            cin >> t_s >> t_e >> m;
            m -= 1;
            for(int k=t_s-540 ; k<t_e-540 ; k++){
                cnt += student[m][k];
            }
            cout << cnt << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[p][1261-540],ch[p][1261-540],b,c,d,e;
		//r(i,0,p)r(j,0,1261-540)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			b-=540;
			//if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			//else{
			//	for(int j=b-1;j>=0;j--){
			//		if(ch[d-1][j]==c)break;
			//		a[d-1][j]++;
			//	}
			//}
		}
		cin>>q;
		//while(q--){
		//	int s=0;
		//	cin>>b>>c>>d;
		//	b-=540;
		//	c-=540;
		//	r(i,b,c)if(a[d-1][i])s++;
		//	cout<<s<<endl;
		//}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265


void f(int n,int m){
  int v[1300][1100]={};
  bool e[1300]={};
  int r;
  cin>>r;
  rep(i,r){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    e[c]=true;
    if(d==1){
      v[c][a]++;
    }
    if(d==0){
      v[c][a]--;
    }
  }

  rep(i,1300){
    if(!e[i]) continue;
    FOR(j,540,1100){
      v[i][j]+=v[i][j-1];
    }
    FOR(j,540,1100){
      v[i][j]=(v[i][j]>0);
    }
    FOR(j,540,1100){
      v[i][j]+=v[i][j-1];
    }
  }


  int q;
  cin>>q;
  rep(i,q){
    int a,b,c;
    cin>>a>>b>>c;
    cout<<v[c][b-1]-v[c][a-1]<<endl;
  }

}


int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0) break;
    f(n,m);
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

#define rep2(i, b, n) for(int i=b; i < n; ++i)
#define rep(i, n) rep2(i, 0, n)
#define sz(x) x.size()
#define pb(x) push_back(x)
#define mk(x, y) make_pair(x, y)
#define B first
#define E second

using namespace std;

typedef pair<int, int> pii;

int main( void )
{
	int n, m;
	int cnt[1000];
	vector<pii> v[1000];
	while(cin >> n >> m, n | m) {
		fill(cnt, cnt+1000, 0);
		rep(i, n) v[i].clear();

		int r; cin >> r;
		rep(i, r) {
			int tt, nn, mm, ss;
			cin >> tt >> nn >> mm >> ss;
			--mm;
			if(ss) {
				if(cnt[mm] == 0) {
					v[mm].pb(mk(tt, 0));
				}
				++cnt[mm];
			}
			else {
				--cnt[mm];
				if(cnt[mm] == 0) {
					v[mm][sz(v[mm])-1].E = tt;
					//cout << v[mm][sz(v[mm])-1].B << ' ' << v[mm][sz(v[mm])-1].E << endl;
				}
			}
		}
		
		int q; cin >> q;
		rep(i, q) {
			int ts, te, mm, time;
			cin >> ts >> te >> mm;
			--mm;
			time = 0;
			rep(j, sz(v[mm])) {
				if((ts <= v[mm][j].B && v[mm][j].B <= te) ||
					(ts <= v[mm][j].E && v[mm][j].E <= te) ||
					(v[mm][j].B <= ts && te <= v[mm][j].E)
				) {
					time += (min(te, v[mm][j].E) - max(ts, v[mm][j].B));
				}
			}
			cout << time << endl;
		}
	}
	return 0;
}