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 <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
	while(1){
		ll N,M; cin >> N >> M;
		if(!N && !M) break;
		ll r; cin >> r;
		vvl v(N,vl(725,-1));
		ll now = 0;
		set<P> se;
		while(r--){
			ll t,n,m,s; cin >> t >> n >> m >> s; n--; t -= 540;
			REP(i,now,t){
				for(auto j : se) v[j.first][i] = j.second;
			}
			now = t;
			if(s == 1) se.emplace(n,m);
			else se.erase(P(n,m));
		}
		ll q; cin >> q;
		while(q--){
			ll ts,te,m; cin >> ts >> te >> m;
			ts -= 540; te -= 540;
			ll ans = 0;
			REP(j,ts,te){
				rep(i,N){
					if(v[i][j] == m){
						ans++;
						break;
					}
				}
			}
			cout << ans << "\n";
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
typedef long long i64;
int main(){
  int N, M;
  while(cin>>N>>M){
    if(N==0&&M==0)break;
    int r;
    cin>>r;
    int a[M+1][1500]={0};
    for(int i=0;i<r;i++){
      
      int t,n,m,s;
      
      cin>>t>>n>>m>>s;
      //n ha tshukawanai
      if(s==1){//login

	a[m][t]++;
      }else if(s==0){//logout
	
	a[m][t+1]--;    }
      
    }
    
      repi(i,1,M+1)
	{
	  rep(j,1300)
	    {
	      if(j==0)continue;
	      a[i][j] += a[i][j-1];
	    }
	}

      int q;
      cin >> q;
      rep(i, q)
	{
	  int ts, te, mm;
	  cin >> ts >> te >> mm;
	  int cnt = 0;
	  printf("%d %d\n", ts, te);
	  repi(j, ts+1, te)
	    {
	      //printf("%d\n" ,a[mm][j]);
	      if(a[mm][j]>0)cnt++;
	    }
	  printf("%d\n",cnt);
	}
      
     
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;

bool table[1001];

typedef struct{
  int t, n, m, s;
} Log;

int main()
{
  while(1){
    int N, M;
    cin >> N >> M;
    if(N == 0 && M == 0) break;
    int r;
    cin >> r;
    Log record[r];
    REP(i,r)
      cin >> record[i].t >> record[i].n >> record[i].m >> record[i].s;
    int q;
    cin >> q;
    REP(i,q){
      int ans = 0;
      int ts, te, s;
      cin >> ts >> te >> s;
      fill(table, table + 1001, true);
      FOR(j,ts,te+1)
        table[j] = false;
      REP(j,r){
        if(record[j].m == s && record[j].s == 0){
          for(int k = j - 1; k >= 0; k--){
            if(record[k].n == record[j].n && record[k].m == record[j].m && (record[k].s & 1)){
              FOR(l,record[k].t,record[j].t+1){
                if(!table[l]){
                  table[l] = true;
                }
              }
              break;
            } 
          }
        } 
      }
      int j = ts;
      while(j <= te){
        bool flag = false;
        while(table[j] && j <= te){
          ans++;
          j++;
          flag = true;
        }
        if(flag)
          ans--;
        while(!table[j] && j <= te){
          j++;
        }
      }
      cout << ans << endl;
    }
  }
}