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++: #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;
		vector< vector<short> > a(M, vector<short>(1300));
		int r; cin >> r;
		while (r--) {
			int t, n, m, s; cin >> t >> n >> m >> s;
			m--;
			a[m][t] += (s ? 1 : -1);
		}
		vector< vector<short> > s(M, vector<short>(1300));
		for (int m = 0; m < M; m++)
			for (int t = 1; t < 1300; t++)
				s[m][t] = s[m][t - 1] + a[m][t - 1];
		int q; cin >> q;
		while (q--) {
			int ts, te, m; cin >> ts >> te >> m;
			m--;
			int cnt = 0;
			for (int t = ts + 1; t <= te; t++)
				if (s[m][t]) cnt++;
			cout << cnt << endl;
		}
	}
}

C++: #include <cstdio>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <array>

using namespace std;

#define all(c) begin(c), end(c)
#define dump(c) cerr << "> " << #c << " = " << (c) << endl
#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const double EPS = 1e-10;


template<typename T1,typename T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p){
  return os << '(' << p.first <<',' << p.second << ')';
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a){
  os << '[';
  rep(i,a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int cnt[10010], st[10010], acc[10010][1300];

int main() {
  for(int N, M; cin >> N >> M, (N | M) != 0; ) {
    memset(cnt, 0, sizeof(cnt));
    memset(st, 0, sizeof(st));
    memset(acc, 0, sizeof(acc));
    int r; cin >> r;
    rep(i, r) {
      int t, n, m, s; cin >> t >> n >> m >> s;
      if (s) {
        if(cnt[m] == 0) st[m] = t;
        cnt[m]++;
      }
      else {
        cnt[m]--;
        if(cnt[m] == 0) {
          REP(j, st[m], t) acc[m][j] = 1;
        }
      }
    }

    int q; cin >> q;
    rep(i, q) {
      int s, e, m; cin >> s >> e >> m;
      int cnt = 0;
      REP(i, s, e) cnt += acc[m][i];
      cout << cnt << endl;
    }
  }
  
  
  return 0;
}

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

int N, M, r, t, n, m, s, q, ts, te;
int d[10001][722];
int cum[10001][722];

int main() {
    int N, M;
    while (cin >> N >> M, N || M) {
        memset(d, 0, sizeof d);
        cin >> r;
        for (int i=0; i<r; ++i) {
            cin >> t >> n >> m >> s;
            d[m][t-540] += (s == 1 ? 1 : -1);
        }
        memcpy(cum, d, sizeof d);
        for (int i=1; i<=M; ++i) {
            for (int j=1; j<722; ++j) {
                d[i][j] = d[i][j-1] + d[i][j];
                cum[i][j] = cum[i][j-1] + (0 < d[i][j]);
            }
        }
        cin >> q;
        for (int i=0; i<q; ++i) {
            cin >> ts >> te >> m;
            if (ts - 541 < 0) {
                cout << cum[m][te-541] << endl;
            } else {
                cout << cum[m][te-541] - cum[m][ts-541] << endl;
            }
        }
    }
    return 0;
}