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 <vector>
using namespace std;
int main(int argc, char *argv[])
{
  for(;;) {
    int N, M;
    cin >> N >> M;
    if(N == 0) break;
    int r;
    cin >> r;
    vector<vector<int>> log;
    for(int i = 0; i < r; i++) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      log.push_back({t, n, m, s});
    }
    int q;
    cin >> q;
    
    for(int i = 0; i < q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int ans = 0, t = 0, in = 0;
      for(vector<int> v : log) {
        if(v[2] == m) {
          if(v[3] == 1) {
            in++;
            if(in == 1) t = v[0];
          } else {
            in--;
            if(in == 0) {
              int tt = v[0];
              if(t < ts) t = ts;
              if(tt > te) tt = te;
              ans += max(0, tt - t);
            }
          }
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
	while(true) {
		ll n, m, r;
		cin>>n>>m;
		if(n == 0 && m == 0) break;
		cin>>r;
		vector<vector<int> > h(m, vector<int>(721, 0));
		REP(i, r) {
			ll t, gomi, man, s;
			cin>>t>>gomi>>man>>s;
			--man;
			t -= 540;
			if(s == 1) {
				h[man][t] += 1;
			} else {
				if(t + 1 <= 720) h[man][t + 1] -= 1;
			}
		}
		REP(i, m) {
			FOR(j, 1, 721) {
				h[i][j] += h[i][j - 1];
			}
		}
		ll q;
		cin>>q;
		REP(i, q) {
			ll ts, te, man;
			cin>>ts>>te>>man;
			--man;
			ts -= 540;
			te -= 540;
			ll ans = 0;
			bool det = false;
			FOR(j, ts, te + 1) {
				if(h[man][j] != 0) {
					++ans;
					det =true;
				} else if(det) {
					det = false;
					--ans;
				}
			}
			if(det) --ans;
			cout<<ans<<endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
enum{LOGOUT,LOGIN};
const int MAX_T=1260+1-540;
int tomin(int d){
     return d-540;
}

int main(){
     int N,M;
     while(cin>>N>>M,N||M){
	  int r;
	  cin>>r;
	  bool table[M+1][MAX_T];
	  for(int i=0;i<M+1;i++){
	       for(int j=0;j<MAX_T;j++){
		    table[i][j]=false;
	       }
	  }
	  int login[M+1][N];
	  for(int i=0;i<r;i++){
	       int t,n,m,s;
	       cin>>t>>n>>m>>s;
	       t=tomin(t);
	       switch(s){
	       case LOGIN:
		    login[m][n]=t;
		    break;
	       case LOGOUT:
		    fill(&table[m][login[m][n]],&table[m][t],true);
		    break;
	       }
	  }
	  int q;
	  cin>>q;
	  for(int i=0;i<q;i++){
	       int ts,te,m;
	       cin>>ts>>te>>m;
	       ts=tomin(ts);te=tomin(te);
	       cout<<count(&table[m][ts],&table[m][te],true)<<endl;
	  }
     }
}