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;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265


void f(int n,int m){
  int v[1300][1100]={};

  int r;
  cin>>r;
  rep(i,r){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if(d==1){
      v[c][a]++;
    }
    if(d==0){
      v[c][a]--;
    }
  }

  rep(i,1300){
    FOR(j,540,1100){
      v[i][j]+=v[i][j-1];
    }
    FOR(j,540,1100){
      v[i][j]=(v[i][j]>0);
    }
    FOR(j,540,1100){
      v[i][j]+=v[i][j-1];
    }
  }


  int q;
  cin>>q;
  rep(i,q){
    int a,b,c;
    cin>>a>>b>>c;
    cout<<v[c][b-1]-v[c][a-1]<<endl;
  }

}


int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0) break;
    f(n,m);
  }

  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <fstream>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
typedef long long ll;

#define INF 1000000000
#define ST 540

short t[1000][1000];

int main(){
    int n,m;
    while(cin>>n>>m, n|m){
        fill(t[0],t[1000], 0);
        int r;
        cin>>r;
        rep(i,r){
            int ti,a,b,s;
            scanf("%d %d %d %d\n", &ti, &a, &b, &s);
            if(s==0) t[b-1][ti-ST] += -1;
            else t[b-1][ti-ST] += 1;
        }

        // imosu
        rep(i,1000){
            rep(j,800){
                t[i][j+1] += t[i][j];
            }
        }

        int q;
        cin>>q;
        rep(i,q){
            int s,e,a;
            scanf("%d%d%d",&s,&e,&a);
            s-=ST; e-=ST;
            int res=0;
            for(int j=s; j<e; j++){
                if(t[a-1][j]>0) res++;
            }
            cout<<res<<endl;
        }
    }
    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[10000][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<cstring>
using namespace std;
int student[10000][721];
int main(){
  int N,M;
  while(cin>>N>>M){
    if(N==0)break;
    int r,q;
    memset(student,-1,sizeof(student));  
    cin>>r;
    for(int i=0;i<r;i++){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      t-=540;m--;
      if(s==1){
	student[m][t]=n;
      }else{
	while(t>0 && student[m][--t]!=n)
	  if(student[m][t]<=0) student[m][t]=n;
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      int ts,te,m,ans=0;
      cin>>ts>>te>>m;
      ts-=540;te-=540;m--;
      for(int i=ts;i<te;i++){
	if(student[m][i]>0)ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[p][1261-540],ch[p][1261-540],b,c,d,e;
		//r(i,0,p)r(j,0,1261-540)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			b-=540;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			b-=540;
			c-=540;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}