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 <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
const ll MOD = 1000000007;
const int MAXM = 10000;
 
int n, m, r, q;
vector<pair<int, int> > logs[MAXM + 1];
 
 
int solve(int sta, int end, int stu){
    sort(logs[stu].begin(), logs[stu].end());
    int cnt = 0, left = 0, res = 0;
    for(pair<int, int> p: logs[stu]){
        if(p.second == 1){
            // login
            if(cnt == 0){
                left = p.first;
            }
            cnt++;
        }else{
            // logout
            cnt--;
            if(cnt == 0){
                res += max(min(end, p.first) - max(sta, left), 0);
            }
        }
    }
    return res;
}
 
 
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    while(1){
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        cin >> r;
        for(int i=0;i<r;i++){
            int tt, tn, tm, ts;
            // time, PC, student, login or logout
            cin >> tt >> tn >> tm >> ts;
            logs[tm].push_back(make_pair(tt, ts));
        }
        cin >> q;
        for(int i=0;i<q;i++){
            int sta, end, stu; cin >> sta >> end >> stu;
            cout << solve(sta, end, stu) << endl;
        }
        for(int i=1;i<m;i++){
            logs[i].clear();
        }
    }
}

C++: #include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

struct data{
  int T, N, S;
};
bool myc(data a, data b){
  if(a.N != b.N)return a.N < b.N;
  return a.T < b.T;
}

signed main(void){
  int i, j, k;
  for(int testcase = 0;testcase < 1234567;testcase++){
    for(;;){
      int n, m, r, q;
      cin >> n >> m; if(!(n | m))break;
      cin >> r;
      vector<data> lis[10000];

      rep(i, r){
        int4(TT, NN, MM, SS);
        lis[MM - 1].pb((data){TT - 540, NN - 1, SS});
      }

      cin >> q;
      for(;q--;){
        int3(ts, te, M);
        M--;
        int use[660] = {0};
        rep(i, lis[M].size()){
          if(lis[M][i].S)use[lis[M][i].T] = 1;
          else use[lis[M][i].T] = -1;
        }
        rep1(i, 660)use[i] += use[i - 1];
        int res = 0;
        for(i = ts;i < te;++i)if(use[i - 540])++res;
        pri(res);
      }
    }


/*/

//*/ break;
  }
  return 0;
}

C++: #include<stdio.h>

int max(int a,int b){
  if(a > b)return a;
  else return b;
}
 
int min(int a,int b){
  if(a < b)return a;
  else return b;
}
 
int t[1000],n[1000],m[1000],s[1000];
int N,M,r,q,begin,end,id;

int main(){
  int i,j,k;
   
  while(1){
    scanf("%d %d",&N,&M);
    if(N==0&&M==0)break;
    scanf("%d",&r);  
    for(i=0;i<r;i++){
      scanf("%d %d %d %d",&t[i],&n[i],&m[i],&s[i]);
    }
     
    scanf("%d",&q);
    for(i=0;i<q;i++){
       
      scanf("%d %d %d",&begin,&end,&id);
       
      int ans=0,A=0,B;
       
      for(j=0;j<r;j++){
        if(m[j]!=id)continue;
         
        if(s[j]==1){
          //login
          A++;
          if(A==1){
            B=t[j];
          }
           
        }else{
          //logout
          A--;
          if(A==0&&begin<=t[j]&&B<=end){
            ans+=(min(end,t[j])-max(begin,B));
          }
           
        }
      }
       
      printf("%d\n",ans);
    }
  }
  return 0;  
}