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 <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool table[10001][1261];

int main () {
	int N, M;
	while (cin >> N >> M, N || M) {
		int lastin[N + 1];
		int r;
		memset(table, 0, sizeof(table));
		cin >> r;
		for (int i = 0; i < r; i++) {
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if (s) {
				lastin[n] = t;
			}
			else {
				fill(table[m] + lastin[n], table[m] + t, true);
			}
		}
		int q;
		cin >> q;
		for (int i = 0; i < q; i++) {
			int s, e, m;
			cin >> s >> e >> m;
			cout << count(table[m] + s, table[m] + e, true) << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main(){
	while(1){
		int N, M; cin >>N>>M;
		if(!N && !M) break;

		int r; cin >>r;
		int pc[1001];
		static int time[10001][1261];
		fill(time[0], time[10000], 0);
		for(int i=0; i<r; i++){
			int t, n, m, s; cin>>t>>n>>m>>s;
			if(s==1){
				pc[n] = t;
			} else{
				for(int j=pc[n]; j<t; j++){
					time[m][j] = 1;
				}
			}
		}

		int q; cin >> q;
		for(int i=0; i<q; i++){
			int s, e, m; cin>>s>>e>>m;
			int count=0;
			for(int j=s; j<e; j++){
				count += time[m][j];
			}
			cout << count << 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;
		}
	}
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main(){
  int t[1123],n[1123],m[1123],s[1123],N,M,r,i,j,k;
  int stud[1269];
  while(1){
    cin>>N>>M;
    // cout<<N<<","<<M<<endl;
    if(N==0 && M==0)
      break;
    cin>>r;
    for(i=0;i<r;i++){
      cin>>t[i]>>n[i]>>m[i]>>s[i];
    }
    int q,ts,te,mm,re=0;
    cin>>q;
    for(i=0;i<q;i++){
      re=0;
      cin>>ts>>te>>mm;
      fill(stud,(stud+1269),0);
      for(j=0;j<r;j++)
	if(m[j]==mm)stud[t[j]]+=(s[j]?1:-1);
      for(j=0;j<=1260;j++){
	stud[j+1]+=stud[j];
	re+=(stud[j]>0)&&(ts<=j)&&(j<te);
      }
      cout<<re<<endl;
    }
   }
  return 0;
}

C++: #include <bits/stdc++.h> // {{{
// clang-format off
#define ARG4(_1, _2, _3, _4, ...) _4
#define rep(...) ARG4(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define REP(i, a) FOR(i, 0, a)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(...) ARG4(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define RREP(i, a) RFOR(i, 0, a)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (int)(a); --i)
#define ALL(c) (c).begin(), (c).end()
#define pb emplace_back
#define em emplace
#define fi first
#define se second
#define USE1(...) template <typename __VA_ARGS__> inline
#define USE2(T, ...) USE1(T, typename __VA_ARGS__)
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
 
using ll = long long;
template<typename T> using vec = std::vector<T>;
USE2(T,U)bool chmax(T&x,U a){return x<a&&(x=a,1);}
USE2(T,U)bool chmin(T&x,U a){return a<x&&(x=a,1);}
USE1(T=int)T in(){T x;std::cin>>x;return x;}
USE2(T=int,V=vec<T>)V reserved(int n){V v;v.reserve(n);return v;}
USE1(T=int)vec<T>in(int n){auto v=reserved<T>(n);rep(i,n)v.pb(in<T>());return v;}
USE2(T,U=std::pair<T,int>)vec<U>enume(const vec<T>&x,int s=0){int N=x.size();auto v=reserved<U>(N);rep(i,N)v.pb(x[i],s+i);return v;}
USE1(T)vec<T>ndvec(T v,int n){return vec<T>(n,v);}
USE2(T,...Ts)auto ndvec(T v,int n,Ts...ns)->vec<decltype(ndvec(v,ns...))>{return ndvec(ndvec(v,ns...),n);}
USE1(T)void pr(T x){std::cout<<x<<'\n';}
USE2(T,...Ts)void pr(T x,Ts...xs){std::cout<<x<<' ';pr(xs...);}
USE1(T=int)T rd(){T x=0,m=0,k;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||'9'<k)break;x=x*10+k-'0';}return x;}
USE1(T=int)void wr(T x,char c='\n'){int s=0,m=0;char b[32];if(x<0)m=1,x=-x;for(;x;x/=10)b[s++]=x%10;if(!s)b[s++]=0;if(m)mypc('-');for(;s--;)mypc(b[s]+'0');mypc(c);}
// clang-format on
// }}}
struct IoSetup { // {{{
  IoSetup() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.precision(10);
    std::cerr.precision(10);
  }
} iosetup; //}}}
using namespace std;
const int inf = 1001001001;
const ll infl = 1001001001001001001ll;
const int dd[] = {0, 1, 0, -1, 0};

struct Record {
  int t, pc, student, login;
  Record(int t, int pc, int student, int login) : t(t), pc(pc), student(student), login(login) {}
};

signed main() {
  int N, M;
  while (N = in(), M = in(), N || M) {
    int r = in();
    auto record = reserved<Record>(r);
    rep(i, r) {
      int t = in(), n = in(), m = in(), s = in();
      record.pb(t, n, m, s);
    }
    int q = in();
    rep(i, q) {
      int ts = in(), te = in(), m = in();
      vec<int> imos(2048, 0);
      for (auto&& rec : record) {
        if (rec.student != m) continue;
        if (rec.login) {
          imos[rec.t] += 1;
        } else {
          imos[rec.t] -= 1;
        }
      }
      int ans = 0, act = 0;
      rep(t, 1300) {
        act += imos[t];
        if (ts <= t && t < te) {
          if (act) ans++;
        }
      }
      pr(ans);
    }
  }
  return 0;
}