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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#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>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
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 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);

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

  int N, M, R;
  while(cin >> N >> M, N > 0){
	cin >> R;
	int t[1001], n[1001], m[1001], s[1001];
	REP(i,R)
	  cin >> t[i] >> n[i] >> m[i] >> s[i];
	
	int Q; cin >> Q;
	REP(q,Q){
	  int st, et, x; cin >> st >> et >> x;
	  int sum = 0, ans = 0, prv = 0;
	  REP(i,R){
		if(m[i] == x){
		  if(s[i]){
			if(sum == 0) prv = t[i];
			++sum;
		  }
		  else{
			--sum;
			if(sum == 0){
			  if(st <= t[i] && t[i] <= et)
				ans += t[i] - max(st, prv);
			  if(t[i] > et && prv <= et)
				ans += et - max(st, prv);
			}
		  }
		}
	  }
	  cout << ans << endl;
	}
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

struct S {
	int t, n, m, s;
};
vector<S> rec;

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

	int N, M, r, q;
	while(cin >> N >> M, N | M) {
		cin >> r;
		rec.clear();

		for(int i = 0; i < r; i++) {
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			rec.push_back(S{ t, n, m, s });
		}

		cin >> q;
		while(q--) {
			int ts, te, m;
			cin >> ts >> te >> m;
			int s = ts, c = 0;
			int sum = 0;
			for(int i = 0; i < r; i++) {
				if(rec[i].m != m) continue;
				if(rec[i].t > te) break;
				if(rec[i].s == 0) {
					c--;
					if(c == 0 && ts <= rec[i].t) {
						sum += rec[i].t - s;
					}
				}
				else {
					if(c == 0 && ts <= rec[i].t) {
						s = rec[i].t;
					}
					c++;
				}
			}
			if(c) {
				sum += te - s;
			}
			cout << sum << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;

const int MAX_M = 10001;
const int MAX_N = 1001;
struct LOG{
	int m;
	int in;
	int out;
};

bool comp(LOG l1, LOG l2){
	return l1.in < l2.in;
}

int main(){
	int N, M;
	while(cin >> N >> M){
		if(N == 0 && M == 0) break;
		vector<LOG> vl[MAX_M];
		stack<LOG> sl[MAX_N];
		int r, q;
		cin >> r;
		for(int i = 0; i <r; i++){
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			LOG l;
			if(s){
				l.in = t;
				sl[n].push(l);
			}else{
				l = sl[n].top();
				sl[n].pop();
				l.out = t;
				vl[m].push_back(l);
			}
		}
		cin >> q;
		for(int i = 0; i < q; i++){
			int ts, te, m;
			cin >> ts >> te >> m;
			int time = 0;
			sort(vl[m].begin(), vl[m].end(), comp);
			for(vector<LOG>::iterator iter = vl[m].begin();
				iter != vl[m].end(); iter++)
			{
				int ti = (*iter).in;
				int to = (*iter).out;
				if(ti >= te) break;
				if(ts >= to) continue;
				if(ts >= ti && te <= to){
					time += te - ts;
					break;
				}else if(ts >= ti && te >= to){
					time += to - ts;
					ts = to;
				}else if(ts <= ti && te <= to){
					time += te - ti;
					break;
				}else{
					time += to - ti;
					ts = to;
				}
				if(ts >= te) break;
			}
			cout << time << endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int r, q, qs, qe, qm;
	while(scanf("%*d%*d%d", &r) == 1){
		vector<int> t(r), m(r), s(r);
		for(int i = 0; i < r; ++i){
			scanf("%d%*d%d%d", &t[i], &m[i], &s[i]);
		}

		scanf("%d", &q);
		for(int i = 0; i < q; ++i){
			scanf("%d%d%d", &qs, &qe, &qm);
			int ans = 0;
			int cnt = 0;
			int prv = qs;
			for(int i = 0; i < t.size(); ++i){
				int u = min(t[i], qe);
				if(cnt){ ans += max(u - prv, 0); }
				if(m[i] == qm){
					if(s[i]){ ++cnt; }
					else{ --cnt; }
				}
				prv = max(prv, u);
			}
			printf("%d\n", ans);
		}
	}
}