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;

bool time[10000+1][1260 - 540 + 1];

void solve()
{
	memset(time,0,sizeof(time));

	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;
	}

	int count[10001]={0},t[10001] = {0};
	for(int j = 0; j < r; j++){
		int m = logs[j].m;
		if(count[m] == 1 && logs[j].s == 0)
			for(int k = t[m]; k < logs[j].t - 540; k++)
				time[m][k] = true;

		if(count[m] == 0 && logs[j].s == 1)
			t[m] = logs[j].t - 540;
		count[m] += (logs[j].s == 1) ? 1 : -1; //ツカツウツδ督トツづーツ堕敖個クツつウツつケツづゥ
	}

	cin >> q;
	for(int i = 0;i < q; i++){
		int ts,te,m;
		cin >> ts >> te >> m;
		int ans = 0;
		for(int i = ts - 540; i < te - 540; i++){
			ans += time[m][i];
		}
		cout << ans << endl;
	}


}

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

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

const double pi  = 2 * acos(0.0);
const double eps = 1e-8;

#define REP(i,a,b) for(int i=(a); i<(b);++i)
#define rep(i,n) REP(i,0,n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst; Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge>  Edges;
typedef vector<Edges> Graph;
typedef vector<Cost>  Array;
typedef vector<Array> Matrix;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

int pc_start_time[1000];
short time_flow[1000][761];

int main(void) {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int N, M;
  while(cin >> N >> M, N || M) {
    for (int i = 0; i < 1000; i++) pc_start_time[i] = -1;
    rep(i, 1000) rep(j, 761) time_flow[i][j] = 0;
    
    int r, q;
    cin >> r;
    for (int i = 0; i < r; i++) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      t -= 540;
      n--; m--;
      if (s == 0) {
        for (int j = pc_start_time[n]; j < t; j++)
          time_flow[m][j] = 1;
      } else {
        pc_start_time[n] = t;
      }
    }

    cin >> q;
    for (int i = 0; i < q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m;
      ts -= 540;
      te -= 540;
      m--;
      int ans = 0;
      for (int j = ts; j < te; j++) if (time_flow[m][j] == 1) ans += 1;
      cout << ans << endl;
    }
  }
  return 0; 
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#define rep(i,a,b) for(int i=(a); i<(b); i++)

using namespace std;
int N , M;

typedef struct data {
  int time, pc, user, on;
} data;

int main(){
  while(cin >> N >> M , N + M){
    data input[1000];
    int r; cin >> r;
    rep(i,0,r){
      int time , pc , user , on;
      cin >> input[i].time >> input[i].pc >> input[i].user >> input[i].on;
    }
    int q; cin >> q;
    rep(i,0,q){
      int ans = 0; int on = 0;
      int start_pc;
      int ts, te, user; cin >> ts >> te >> user;
      rep(j,0,r){
        if(input[j].user != user) continue;
        if(input[j].on){
          if(on == 0) start_pc = input[j].pc;
          on++;
        }
        else on--;
        if(!on){
          int k = j - 1;
          while(!(input[k].pc == start_pc && input[k].on)) k--;
          int start = max(input[k].time , ts);
          int end = min(input[j].time , te);
          if(end - start >= 0) ans += end - start;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}