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 <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX/3

#define MAX_N 1000

int use[11111][1300];
int N,M,r,q;

int main(){
	cin.sync_with_stdio(false);
	while(1){
		cin>>N>>M;
		if(N==0)break;
		memset(use,0,sizeof(use));
		cin>>r;
		rep(i,r){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			n--; m--;
			if(s==1){
				use[m][t]+=1;
			}else{
				use[m][t]-=1;
			}
		}
		rep(i,M)repl(j,540,1300){
			use[i][j+1]+=use[i][j];
		}
		cin>>q;
		rep(i,q){
			int ts,te,m;
			cin>>ts>>te>>m;
			m--;
			int res=0;
			repl(j,ts,te){
				if(use[m][j]>0)res++;
			}
			cout<<res<<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));
		for(i=1;i<=M;i++) bm[i].reset();
		cin>>r;
		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++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	for (;;) {
		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;
		while (q--) {
			int ts, te, _m; cin >> ts >> te >> _m;
			int x = 0, start, end, ans = 0;
			for (int i = 0; i < r; i++) {
				if (m[i] != _m) continue;
				if (s[i]) {
					if (x == 0) start = t[i];
					x++;
				}
				else {
					x--;
					if (x == 0) {
						end = t[i];
						ans += min(te, end) - max(ts, start);
					}
				}
			}
			cout << ans << endl;
		}
	}
}

C++: #include <iostream>

using namespace std;

int st[10000][1261] = {0};
int main() {
	int N, M;
	while ( cin >> N >> M, N || M ) {
		fill(&st[0][0], &st[0][0]+10000*1261, 0);
		int r; cin >> r;
		for (int i = 0; i < r; ++i) {
			int t, n, m, s; cin >> t >> n >> m >> s; --n, --m;
			if (s == 0) { // logout
				st[m][t] = -1;
			}
			else {
				st[m][t] = 1;
			}
		}
		for (int i = 0; i < M; ++i) {
			for (int j = 1; j <= 1260; ++j) {
				st[i][j] += st[i][j-1];
			}
		}
		for (int i = 0; i < M; ++i) {
			for (int j = 1; j <= 1260; ++j) {
				st[i][j] = st[i][j] ? 1 : 0;
			}
		}
		for (int i = 0; i < M; ++i) {
			for (int j = 1; j <= 1260; ++j) {
				st[i][j] += st[i][j-1];
			}
		}
		int q; cin >> q;
		for (int t = 0; t < q; ++t) {
			int s, e, m; cin >> s >> e >> m; --m;
			cout << st[m][e-1]-st[m][s-1] << endl;
		}
	}
}

C++: #include<iostream>
#include<string.h>
#include<string>
#include<sstream>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  while(1){
    int ni,mi;
    int i,j,k;
    cin>>ni>>mi;
    if(ni==0 && mi==0)break;
    int r,q;
    int record[1005][800]={};//n,t
    cin>>r;
    int t,n,m,s;
    rep(i,r){
      cin>>t>>n>>m>>s;
      if(s)
	record[n][t-500]+=m;
      else
	record[n][t-500]-=m;
    }
    rep(i,ni+1)rep(j,790)
      record[i][j+1]+=record[i][j];
 
    cin>>q;
    rep(i,q){
      int ts,te,tm;
      cin>>ts>>te>>tm;
      int ans=0;
      for(j=ts;j<te;j++){
	bool temp=false;
	rep(k,ni+1)
	  if(record[k][j-500]==tm)temp=true;
	if(temp)ans++;
      }
      cout<<ans<<endl;
    }

  }
}