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 <limits.h>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

int N,M,r,q;
typedef struct{
	int t,n,m,s;
} LOG;

void solve()
{
	cin>>r;
	vector<LOG> logs(r);
	for(int i = 0;i < r; i++){
		cin >> logs[i].t >> logs[i].n >> logs[i].m >> logs[i].s;
	}
	cin >> q;
	for(int i = 0;i < q; i++){
		int ts,te,m;
		cin >> ts >> te >> m;
		int count = 0,ans = 0,t = 0;
		for(int j = 0; j < r; j++){
			if(logs[j].m != m) continue;

			if(logs[j].s == 1 && count == 0){
				t = max(logs[j].t,ts); //Jn
			} else if(logs[j].s == 0 && count == 1){
				ans += max(min(logs[j].t,te) - t,0); 
			}

			if(logs[j].t >= te) break;

			count += (logs[j].s == 1) ? 1 : -1; //JEgð¸³¹é
		}
		cout << ans << endl;
	}


}

int main()
{
	while(cin >>N >>M,(N||M)){
		solve();
	}
	return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    while(true){
        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;
        vector<int> ts(q),te(q),qm(q);
        for(int i=0;i<q;i++){
            cin>>ts[i]>>te[i]>>qm[i];
        }
        for(int i=0;i<q;i++){
            int sum=0;
            int start=0;
            int PCnum=0;
            for(int j=0;j<r;j++){
                if(m[j]==qm[i]){
                    if(s[j]==1){
                        PCnum++;
                        if(PCnum==1)
                            start=t[j];
                    }else{
                        PCnum--;
                        if(PCnum==0)
                            sum+=min(t[j],te[i])-max(ts[i],start);
                    }
                }
            }
            cout<<sum<<endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;



int main(){

	int N,M,r;
	while(cin >> N >> M && !(N == 0 && M == 0)){
		// Ô
		vector<pair<int,int> > Gakusei[10001];
		cin >> r;
		pair<int,int> pc[1001];
		for(int i = 0; i < r; i++){
			int t;
			int n;
			int m;
			int s;
			cin >> t >> n >> m >> s;
			if(s == 1){
				pc[n].first = t;
			}
			else{
				pc[n].second = t;
				Gakusei[m].push_back(pc[n]);
			}
		}

		int q;
		cin >> q;
		for(int i = 0; i < q; i++){
			int cnt = 0;
			int ts,te,m;
			cin >> ts >> te >> m;
			sort(Gakusei[m].begin(),Gakusei[m].end());
			int cTime = ts;
			for(int j = 0; j < Gakusei[m].size(); j++){
				// æÃ~ÉJnÔª¢©ç©Ä¢­
				if(cTime >= Gakusei[m][j].second)
					continue;
				else{
					cnt += (min(te,Gakusei[m][j].second) - max(cTime,Gakusei[m][j].first));
					cTime = Gakusei[m][j].second;
				}
			}
			cout << cnt << endl;
		}

	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

#ifdef WIN32
#define dump(x) cout << #x << " = " << (x) << "\n"
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif

int N, M, r, q;
int students[ 10001 ][ 1262 ] = {};

void solve() {
  while ( cin >> N >> M && N && M ) {
    cin >> r;
    CLR( students );
    while ( r-- ) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      if ( s ) {
        students[ m ][ t + 1 ]++;
      }
      else {
        students[ m ][ t + 1 ]--;
      }
    }
    for ( int i = 1; i <= M; i++ ) {
      for ( int j = 540; j < 1262; j++ ) {
        students[ i ][ j ] += students[ i ][ j - 1 ];
      }
    }
    cin >> q;
    while ( q-- ) {
      int s, g, m, ans = 0;
      cin >> s >> g >> m;
      for ( int i = s + 1; i <= g; i++ ) {
        ans += !!students[ m ][ i ];
      }
      cout << ans << "\n";
    }
  }
}

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