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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline long toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline long toLong(string s) {long v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(long i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-6;
const long INF = 1e10;

int rng[10002][800];
int main(){
	int N,M,r,q;
	while(cin>>N>>M){
		if(N==0&&M==0)break;
		CLR(rng);
		cin>>r;
		REP(i,r){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			t-=540;
			if(s==1)rng[m][t]++;
			else rng[m][t]--;
		}
		cin>>q;
		REP(i,q){
			int ts,te,m;
			cin>>ts>>te>>m;
			ts-=540;te-=540;
			int sum=0,res=0;
			bool f=false;
			int b=0;
			for(int j=0;j<=te;j++){
				sum+=rng[m][j];
				if(j>=ts){
					if(sum>0){
						if(j==te&&f)res+=(j-b);
						if(!f)b=j;
						f=true;
					}else{
						if(f)res+=(j-b);
						
						f=false;
					}
					
				}
			}
			cout<<res<<endl;
		}
	}
	return 0;
}
/*
1 1
2
600 1 1 1
700 1 1 0
5
540 600 1
550 650 1
610 620 1
650 750 1
700 800 1
*/

C++: #include<iostream>
#include<vector>
#include<set>
#include<functional>
#include<map>
#define MAX 1010
#include<queue>
using namespace std;
typedef pair<int,int> P;
int main(){
	int e,q,t,n,m,s,p;
	while(cin>>e>>q,e||q){
		vector<P> tab[MAX];
		cin>>p;
		for(int i=0;i<p;i++){
			cin>>t>>n>>m>>s;
			tab[m-1].push_back(P(t,s));
		}
		int now,t1,t2;
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>t1>>t2>>m;
			now=t1;
			int ans=0,sum=0;
			int j;
			m--;
			for(j=0;j<tab[m].size()&&tab[m][j].first<t2;j++){
				if(!tab[m][j].second){
					if(sum>0&&tab[m][j].first>=t1&&tab[m][j].first<t2){
						ans+=tab[m][j].first-now;
					}sum--;
					if(tab[m][j].first>t1&&tab[m][j].first<t2)
					now=tab[m][j].first;
				}
				else if(tab[m][j].second){
					if(sum>0&&tab[m][j].first>t1&&tab[m][j].first<t2){
						ans+=tab[m][j].first-now;
					}sum++;
					if(tab[m][j].first>=t1&&tab[m][j].first<t2)
					now=tab[m][j].first;
				}
			}
			if(sum>0)
			ans+=t2-now;
			cout<<ans<<endl;
			
		}
	}
	return 0;
}