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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;


int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	while (1) {
		int n, m; scanf("%d %d", &n, &m);
		if (n == 0 && m == 0)break;
		int offset = 540;
		vector<vector<int>> time(m, vector<int>(750));
		map<pair<int, int>, int> mp;
		int r; scanf("%d", &r);
		for (int i = 0; i < r; i++) {
			int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
			c--;
			if (d == 0) {
				for (int i = mp[{b, c}]; i < a; i++) {
					time[c][i - offset] = 1;
				}
				mp.erase({ b,c });
			}
			else {
				mp[{b, c}] = a;
			}
		}

		int q; scanf("%d", &q);
		while (q--) {
			int ts, te, a;
			scanf("%d %d %d", &ts, &te, &a);
			a--;
			int res = 0;
			for (int i = ts - offset; i < te - offset; i++) {
				res += time[a][i];
			}
			//puts("a");
			printf("%d\n", res);
		}
	}
	return 0;
	/*
		おまじないを使ったらscanfとprintf関連注意！！！！！！！！！！！！
	*/
}

C++: #include <bits/stdc++.h>
using namespace std;

void solve(int n, int m){
	int v[m+1][1300];
	for(int i=0; i<m+1; ++i)
		for(int j=0; j<1300; ++j)
			v[i][j] = 0;
	int r, q;
	cin >> r;
	for(int i=0; i<r; ++i){
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		if(d == 1) ++v[c][a];
		else --v[c][a];
	}
	for(int i=1; i<m+1; ++i)
		for(int j=1; j<1300; ++j)
			v[i][j] += v[i][j-1];
	cin >> q;
	for(int i=0; i<q; ++i){
		int a, b, c;
		cin >> a >> b >> c;
		int ans = 0;
		for(int j=a; j<b; ++j)
			if(v[c][j] > 0)
				++ans;
		cout << ans << "\n";
	}
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int n, m;
		cin >> n >> m;
		if(n == 0 && m == 0) return 0;
		solve(n, m);
	}
}

C++: #include<iostream>
#include<list>
#include<cstring>
#include<algorithm>
#include <utility>
#include<stdio.h>
#include<climits>
#include <vector>
#include <queue>

using namespace std;

int main(void){
    int N,M,r,q;
    int t[1000][4];
    int ts,te;
    int i,j,k,l,m,n;
    while(1){
        cin >> N >> M;
        if(!N) break;
        cin >> r;
        for(i = 0;i < r;i++){
            cin >> t[i][0] >> t[i][1] >> t[i][2] >> t[i][3];
        }
        cin >> q;
        for(i = 0;i < q;i++){
            cin >> ts >> te >> m;
            n = 0;
            l = 0;
            for(j = 0;j < r;j++){
                if(t[j][2] == m){
                    if(n == 0) k = t[j][0];
                    if(t[j][3]){
                        n++;
                    }
                    else{
                        n--;
                    }
                    if(n == 0){
                        if(k < ts) k = ts;
                        if(t[j][0] > te) k = te-k;
                        else k = t[j][0]-k;
                        if(k > 0) l += k;
                    }
                }
            }
            cout << l << endl;
        }
    }
}

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

int student[10001][822]; //imos法のための配列

int main(){
	while(1){
		int N, M; //pc数，学生数
		
		scanf(" %d %d", &N, &M);
		if(N==0) break;
		
		for(int i=1; i<=M; ++i)
		for(int j=0; j<=821; ++j)
		student[i][j]=0;
		
		int r;
		scanf(" %d", &r);
		for(int i=0; i<r; ++i){
			int t, n, m, s;
			scanf(" %d %d %d %d", &t, &n, &m, &s);
			if(s==0) student[m][t-540+1]--;
			else student[m][t-540+1]++;	
		}

		for(int i=1; i<=M; ++i)
		for(int j=0; j<821; ++j)
		student[i][j+1] += student[i][j];
		
		//for(int j=0; j<820; ++j) printf("student[%d] = %d\n", j, student[1][j]);
		
		
		int q;
		scanf(" %d", &q);
		for(int i=0; i<q; ++i){
			int ts, te, m;
			scanf(" %d %d %d", &ts, &te, &m);
			
			int count=0;
			for(int j=ts+1; j<=te; ++j) if(student[m][j-540]>0) ++count;
			printf("%d\n", count);
		} 
	
	}
	return 0;
}