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 <cstdio>
#include <vector>
#include <map>

using namespace std;
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int main(){
    int N,M;
    while(scanf("%d%d",&N,&M)!=EOF && N|M){
        typedef pair<int,int> pii;
        typedef vector<pii> vp;
        typedef vector<vp> vvp;
        typedef vector<vvp> vvvp;
        vvvp v(N+1,vvp(M+1));
        int r;scanf("%d",&r);
        rep(i,r){
            int t,n,m,s;
            scanf("%d%d%d%d",&t,&n,&m,&s);
            if(s){
                v[n][m].emplace_back(t,0);
            }else{
                v[n][m].back().second = t;
            }
        }

        int q;scanf("%d",&q);
        rep(i,q){
            int s,e,m;
            int ans=0;
            scanf("%d%d%d",&s,&e,&m);
            continue;
            static bool use[2000]={};
            loop(j,1,N+1)rep(k,v[j][m].size()){
                auto t=v[j][m][k];
                if(e<t.first || t.second<s) continue;
                int E=min(t.second,e);
                int S=max(t.first,s);
                if(S>E)continue;
                loop(w,S,E)use[w]=true;
                // printf("%d %d %d %d\n",S,E,m,x);
            }
            rep(j,2000)if(use[j])ans++;
            cout<<ans<<endl;
        }
    }
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

struct T{
	int a,b,c;
};

int main(){
	int N,M;
	while(cin>>N>>M,N||M){
		int r;
		vector<vector<T> > v(M);
		cin>>r;
		rep(i,r){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			t-=540;
			m--;
			T tmp;
			tmp.a=t;
			tmp.b=n;
			tmp.c=s;
			v[m].PB(tmp);
		}
		int q;
		cin>>q;
		rep(i,q){
			int ts,te,m;
			cin>>ts>>te>>m;
			//cout<<ts<<" "<<te<<" "<<m<<endl;
			ts-=540;
			te-=540;
			m--;
			int ans=0;
			int tt1=0,tt2=0;
			rep(j,v[m].size()){
				if(v[m][j].a>te){
					ans+=te-tt2;
					break;
				}
				if(v[m][j].c==1){
					if(tt1==0){
						if(v[m][j].a<ts){
							tt2=ts;
						}
						else tt2=v[m][j].a;
						tt1++;
					}
					else{
						tt1++;
					}
				}
				else{
					tt1--;
					if(tt1==0){
						if(v[m][j].a>=ts) ans+=v[m][j].a-tt2;
						//cout<<" ans "<<ans<<endl;
					}
				}
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

/* 
775 1
790 2
800 2
810 3
820 1
825 3
860 1
880 1
+50

*/

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <map>
using namespace std;

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

int N, M, r, q;
bool used[10000][2000];

void solve() {
  memset(used, false, sizeof used);
  cin>>r;
  
  map<int,int> login[M];
  rep(i,r) {
    int t, n, m, s;
    cin>>t>>n>>m>>s; m--;
    if (s == 1) {
      login[m].insert(mp(n,t));
    } else {
      map<int,int>::iterator it = login[m].begin();
      while (it != login[m].end()) {
        if (it->first == n) {
          REP(j,it->second,t) used[m][j] = true;
          login[m].erase(it);
          break;
        }
        it++;
      }
    }
  }
  
  cin>>q;
  rep(i,q) {
    int ts, te, m, res = 0;
    cin>>ts>>te>>m; m--;
    REP(i,ts,te) if (used[m][i])
      res++;
    cout<<res<<endl;
  }
}

int main (int argc, char const* argv[]) {
  while (cin>>N>>M, N||M) {
    solve();
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

struct T{
	int a,b,c;
};

int main(){
	int N,M;
	while(cin>>N>>M,N||M){
		int r;
		vector<vector<T> > v(M);
		cin>>r;
		rep(i,r){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			t-=540;
			m--;
			T tmp;
			tmp.a=t;
			tmp.b=n;
			tmp.c=s;
			v[m].PB(tmp);
		}
		int q;
		cin>>q;
		rep(i,q){
			int ts,te,m;
			cin>>ts>>te>>m;
			//cout<<ts<<" "<<te<<" "<<m<<endl;
			ts-=540;
			te-=540;
			m--;
			int ans=0;
			int tt1=0,tt2=0;
			rep(j,v[m].size()){
				if(v[m][j].a>te){
					if(tt1) ans+=te-tt2;
					break;
				}
				if(v[m][j].c==1){
					if(tt1==0){
						if(v[m][j].a<ts){
							tt2=ts;
						}
						else tt2=v[m][j].a;
						tt1++;
					}
					else{
						tt1++;
					}
				}
				else{
					tt1--;
					if(tt1==0){
						if(v[m][j].a>=ts) ans+=v[m][j].a-tt2;
						//cout<<" ans "<<ans<<endl;
					}
				}
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

/* 
775 1
790 2
800 2
810 3
820 1
825 3
860 1
880 1
+50

*/