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 <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <tuple>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define DECIM8  fixed<<setprecision(8) 
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

using namespace std;
int usepc[10010][1310];
int main(void)
{
  for(;;){
    REP(i,10010) REP(j,1310){
      usepc[i][j]=0;
    }
    int N,M;
    cin >> N >> M;
    if(!N&&!M) return 0;
    int r;
    cin >> r;
    REP(i,r){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      usepc[m][t]+=1-2*s;
    }
    REP(i,M+1) REP(j,1300){
      usepc[i][j+1]+=usepc[i][j];
    }
    REP(i,M+1) REP(j,1300){
      if(usepc[i][j+1]) usepc[i][j+1]=usepc[i][j]+1;
      else usepc[i][j+1]=usepc[i][j];
    }
    int q;
    cin >> q;
    REP(i,q){
      int ts,te,m;
      cin >> ts >> te >> m;
      cout << usepc[m][te-1]-usepc[m][ts-1] << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define pb(a) push_back(a)
#define rd(a) cin>>(a)
#define RD(a,b) cin>>(a)>>(b)
#define pr(a) cout<<(a)<<endl
#define PR(a,b) cout<<(a)<<" "<<(b)<<endl
#define F first
#define S second
#define ll long long
bool check(int n,int m,int x,int y){return (x<0||x>=n||y<0||y>=m)?false:true;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
typedef pair<int,int> P;
typedef pair<int,P> P2;

int a[10001][1300];

int main() {
  int n,m;
  while(cin >> n >> m && n) {
    int p,q;
    cin >> p;
    memset(a,0,sizeof(a));
    rep(i,p) {
      int x,y,z,w;
      cin >> x >> y >> z >> w;
      if(w) a[z][x]++;
      else a[z][x]--;
    }
    REP(i,1,m+1) {
      REP(j,1,1300) a[i][j]+=a[i][j-1];
    }
    cin >> q;
    while(q--) {
      int ans=0,t1,t2,x;
      cin >> t1 >> t2 >> x;
      REP(i,t1,t2) ans+=(a[x][i]>0);
      pr(ans);
    }
  }
  return 0;
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>

using namespace std;

#define INF 999999999

vector<int> in[10001], out[10001];

int main(){
	int i, j, k, t, N, M, r, q, n, m, s, ts, te, ans;
	while(1){
		cin >> N >> M;
		if(!N) break;
		for(i=1;i<=M;i++){
			in[i].clear();
			out[i].clear();
		}
		cin >> r;
		for(i=0;i<r;i++){
			cin >> t >> n >> m >> s;
			if(s) in[m].push_back(t);
			else out[m].push_back(t);
		}
		for(i=1;i<=M;i++){
			for(j=0;j<in[i].size();j++){
				for(k=j+1;k<in[i].size();k++){
					if(in[i][j] <= in[i][k] && out[i][k] <= out[i][j]){
						in[i][k] = 0;
						out[i][k] = 0;
					}
					else if(in[i][j] <= in[i][k] && in[i][k] <= out[i][j]){
						out[i][j] = out[i][k];
						in[i][k] = 0;
						out[i][k] = 0;
					}
					else if(in[i][j] <= out[i][k] && out[i][k] <= out[i][j]){
						in[i][k] = in[i][j];
						in[i][j] = 0;
						out[i][j] = 0;
					}
					else if(in[i][k] <= in[i][j] && out[i][j] <= out[i][k]){
						in[i][j] = 0;
						out[i][j] = 0;
					}
				}
			}
		}
		cin >> q;
		for(i=0;i<q;i++){
			ans = 0;
			cin >> ts >> te >> m;
			for(j=0;j<in[m].size();j++){
				if(in[m][j] <= ts && te <= out[m][j]){
					ans += te - ts;
				}
				else if(in[m][j] <= ts && ts <= out[m][j]){
					ans += out[m][j] - ts;
				}
				else if(in[m][j] <= te && te <= out[m][j]){
					ans += te - in[m][j];
				}
				else if(ts <= in[m][j] && out[m][j] <= te){
					ans += out[m][j] - in[m][j];
				}
			}
			//cout << "a:";
			cout << ans << endl;
		}
	}
	return 0;
}