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 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 cnt[1000];
vector<pii> v[1000];

int main( void )
{
	int n, m;
	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;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int N,M;
	while(cin>>N>>M,N) 
	{
		int r;cin>>r;
		int t[r],m[r],s[r];
		int n;

		for(int i = 0; i < r; ++i)
		{
			cin>>t[i]>>n>>m[i]>>s[i];
		}

		int q; cin>>q;

		for(int i = 0; i < q; ++i)
		{
			int ts,te,sn;
			cin>>ts>>te>>sn;
			int ans=0,cnt=0,prev=0;
			for(int j = 0; j < r; ++j)
			{
				if(m[j]==sn)
				{
					if(s[j]==1)
					{
						cnt++;
						if(cnt==1)
						{
							prev=t[j];
						}
					}
					else
					{
						cnt--;
						if(cnt==0)
						{
							if(prev<=ts&&t[j]<=te&&ts<=t[j]) ans+=t[j]-ts;
							else if(ts<=prev&&prev<=te&&te<=t[j]) ans+=te-prev;
							else if(ts<=prev&&t[j]<=te) ans+=t[j]-prev;
							else if(prev<=ts&&te<=t[j]) ans+=te-ts;
						}
					}
				}
			}
			cout<<ans<<endl;
		}
	}
  return 0;
}

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

bitset<720> bm[10001];
int d[1001];

main(){
	int i,r,q,N,M,t,n,m,s,ts,te;
	while(cin>>N>>M,N){
		memset(d, 0, sizeof(d));
		cin>>r;
		for(i=1;i<=N;i++) bm[i].reset();
		while(r--){
			cin >> t >> n >> m >>s;
			t -= 540;
			if(s) d[n] = t;
			else  for(ts=d[n];ts<t;ts++) bm[m].set(ts);
		}
		cin>>q;
		while(q--){
			cin >> ts >> te >> m;
			ts -= 540;
			te -= 540;
			for(s=0;ts<te;ts++) s+=bm[m][ts];
			cout << s << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main(){
	int N,M;
	while(cin>>N>>M){
		if(N==0 && M==0) break;
		int r,q;
		vector<vector<int> >pc(N+1,vector<int>(M+1,0));
		vector<vector<int> >time(M+1,vector<int>(1261,0));
		cin>>r;
		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			if(s==1){
				time[m][t]=1;
				pc[n][m]=t;
			}else if(s==0){
				for(int j=pc[n][m];j<t;j++){
					time[m][j]=1;
				}
				pc[n][m]=0;
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			int ts,te,mn,count=0;
			cin>>ts>>te>>mn;
			for(int j=ts;j<te;j++){
				if(time[mn][j]==1) count++;
			}
			cout<<count<<endl;
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)


typedef pair<int,int> pii;

const int M = 10010;
int in1[M],in2[M],in3[M],in4[M];

int main(){
	
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0 && m==0)break;
		
		int t;
		cin>>t;
		
		rep(i,t){
			cin>>in1[i]>>in2[i]>>in3[i]>>in4[i];
		}
		
		int y;
		cin>>y;
		
		rep(i,y){
			int a,b,c;
			cin>>a>>b>>c;
			
			int pos=0;
			int diff[2000]={0};
			rep(j,t){
				if(in3[j]==c){
					if(in4[j]==0)diff[in1[j]]--;
					else diff[in1[j]]++;
				}
			}
			
			int sum[2000]={0};
			reps(j,1,2000){
				sum[j] = sum[j-1]+diff[j];
			}
			
			int cont=0;
			reps(j,a,b){
				if(sum[j]>0)cont++;
			}
			printf("%d\n",cont);
		}
	}
}

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

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

typedef pair<int,int> P;

struct data{
    int t,pc,stu,s;
};
void solve(){
    int N,M;cin>>N>>M;
    if(N==0)exit(0);
    int r;cin>>r;

    vector<data>v;
    vector<vector<data>>use;
    use.resize(N+1);
    REP(i,r){
        int t,n,m,s;
        cin>>t>>n>>m>>s;
        v.push_back({t,n,m,s});
        use[n].push_back({t,n,m,s});
    }

    int q;cin>>q;
    while(q--){
        int ts,te,m;cin>>ts>>te>>m;
        vector<int>cnt(1261);
        REP(i,N+1){
            for(int j = 0;j<(int)use[i].size();j+=2){
                if(use[i][j].stu==m){
                    for(int t=use[i][j].t;t<(int)use[i][j+1].t;t++)cnt[t]++;
                }
            }
        }
        int res = 0;
        for(int t = ts;t<te;t++){
            if(cnt[t])res++;
        }
        cout<<res<<endl;
    }

}

signed main(){
    while(true)solve();
}