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;

#define max(a,b) (a > b ? a : b)
#define min(a,b) (a < b ? a : b)

typedef struct {
	int m, n, s, d;
} T;

typedef struct {
	int s, d;
} P;

int N, M, r, q;
vector<P> a[10001];
T t[1000];

int closs(T &tm) {
	for (int i = 0; i < a[tm.m].size(); i++) {
		if ((tm.d >= a[tm.m][i].s && tm.d <= a[tm.m][i].d) || (tm.s >= a[tm.m][i].s && tm.s <= a[tm.m][i].d) || (tm.s <= a[tm.m][i].s && tm.d >= a[tm.m][i].d)) {
			return i;
		}
	}
	return -1;
}

int main(){
	int _t, _n, _m, _s, _d;
	int ite, tmp, ans;
	while (cin >> N >> M && N | M != 0) {
		for (int i = 0; i <= M; i++) {
			a[i] = vector<P>();
		}
		ite = 0;
		cin >> r;
		for (int I = 0; I < r; I++) {
			cin >> _t >> _n >> _m >> _s;
			if (_s == 1) {
				t[ite++] = { _m,_n,_t,0 };
			}
			else {
				for (int i = ite - 1; i >= 0; i--) {
					if (t[i].m == _m && t[i].n == _n) {
						t[i].d = _t;
						tmp = closs(t[i]);
						if (tmp == -1) {
							a[_m].push_back({ t[i].s,t[i].d });
						}
						else {
							a[_m][tmp].d = max(a[_m][tmp].d, t[i].d);
							a[_m][tmp].s = min(a[_m][tmp].s, t[i].s);
						}
						break;
					}
				}
			}
		}
		cin >> q;
		for (int I = 0; I < q; I++) {
			cin >> _s >> _d >> _m;
			ans = 0;
			for (int i = 0; i < a[_m].size(); i++) {
				if (a[_m][i].s >= _s) {
					if (a[_m][i].d >= _d) {
						ans += _d - a[_m][i].s;
					}
					else if(a[_m][i].s < _d) {
						ans += a[_m][i].d - a[_m][i].s;
					}
				}
				else if (a[_m][i].d <= _d) {
					ans += a[_m][i].d - _s;
				}
				else if(a[_m][i].d > _s) {
					ans += _d - _s;
				}
			}
			cout << ans << endl;
		}
	}

	return 0;
}

C++: //24
#include<iostream>

using namespace std;

struct record{
  int t,n,m,s;
};

int main(){
  for(int n,m;cin>>n>>m,n|m;){
    int r;
    cin>>r;
    record rec[10000];
    for(int i=0;i<r;i++){
      cin>>rec[i].t>>rec[i].n>>rec[i].m>>rec[i].s;
    }
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
      int s,e,mm;
      cin>>s>>e>>mm;
      int ac=0;
      int at=0;
      int idx=0;
      for(int j=540;j<=1260;j++){
	while(idx<r&&rec[idx].t==j){
	  if(rec[idx].m==mm){
	    if(rec[idx].s){
	      ac++;
	    }else{
	      ac--;
	    }
	  }
	  idx++;
	}
	at+=ac&&s<=j&&j<e;
      }
      cout<<at<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#define NUMBER 10000
#define TIME 1261
using namespace std;

int record[NUMBER][TIME];
int rec[1000][4];
int quest[50][3];

void answer_question(int q) {
  for (int i = 0; i < q; i++) {
    int student = quest[i][2];
    int count = 0;
    for (int j = quest[i][0]; j < quest[i][1]; j++)
      if (record[student][j]) count++;
    cout << count << endl;
  }
}

void record_search(int r) {
  for (int i = 0; i < r; i++) {
    if (rec[i][3] == 0) continue;
    for (int j = i + 1; j < r; j++) {
      if (rec[j][1] == rec[i][1] && rec[j][2] == rec[i][2] && rec[j][3] == 0) {
	for (int k = rec[i][0]; k < rec[j][0]; k++) {
	  record[rec[i][2]][k] = 1;
	}
	goto label;
      }
    }
  label: ;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;

    int r, q;
    cin >> r;
    for (int i = 0; i < r; i++)
      cin >> rec[i][0] >> rec[i][1] >> rec[i][2] >> rec[i][3];

    cin >> q;
    for (int i = 0; i < q; i++)
      cin >> quest[i][0] >> quest[i][1] >> quest[i][2];

    for (int i = 0; i < r; i++)
      for (int j = 0; j < TIME; j++)
	record[i][j] = 0;
    record_search(r);
    answer_question(q);
  }
}

C++: #include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

int N,M,r,q;
int cum[10010][1300];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  while (cin >> N >> M, N) {
    ZERO(cum);
    cin >> r;
    REP(i,r) {
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      m--;
      if (s == 1) cum[m][t]++;
      else cum[m][t]--;
    }
    REP(i,M) {
      for (int j = 0; j+1 < 1300; j++) {
        cum[i][j+1] += cum[i][j];
      }
    }
    cin >> q;
    REP(i,q) {
      int ts,te,m;
      cin >> ts >> te >> m;
      m--;
      int ans = 0;
      for (int j = ts; j < te; j++) {
        if (cum[m][j] > 0) ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}