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;

const int MAX_N = 1010;
const int MAX_M = 10010;
const int MAX_TIME = 730;
int T[MAX_M][MAX_TIME];
int main()
{
    int N, M, r, q;
    while (cin >> N >> M, N || M)
    {
        // T配列の初期化
        for (int i = 0; i < M; ++i)
        {
            memset(T[i], 0, sizeof(T[i]));
        }
        // T[i][j] := i番目(0-indexed)の学生が何台のPCにログインしているか
        //            時刻[j, j+1)に
        cin >> r;
        while (r--)
        {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            --m;
            t -= 540; // 540 <= t <= 1260 を調整
            if (s == 1)
            {
                ++T[m][t];
            }
            else
            {
                --T[m][t];
            }
        }
        // 累積和にする
        for (int i = 0; i < M; ++i)
        {
            for (int j = 1; j < MAX_TIME; ++j)
            {
                T[i][j] += T[i][j - 1];
            }
        }
        int q;
        cin >> q;
        while (q--)
        {
            int ts, te, m;
            cin >> ts >> te >> m;
            --m;
            ts -= 540, te -= 540;
            int res = 0;
            for (int t = ts; t < te; ++t)
            {
                if (T[m][t] >= 1)
                    ++res;
            }
            cout << res << endl;
        }
    }
    return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000

typedef long long ll;
typedef pair<int, int> P;

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main(void) {
    int N, M, r;
    while (1) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        cin >> r;
        vector<P> students[M+1];
        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            students[m].push_back(make_pair(t, s));
        }
        for (int i = 1; i <= M; i++) {
            sort(students[i].begin(), students[i].end());
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int s, e, m;
            cin >> s >> e >> m;
            int time = s, cnt = 0, ans = 0;
            for (int j = 0; j < students[m].size(); j++) {
                P p = students[m][j];
                if (time <= p.first && cnt > 0) {
                    ans += min(p.first, e) - time;
                }
                time = max(s, min(p.first, e));
                cnt += p.second ? 1 : -1;
                if (time == e) break;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#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 <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
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(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M, R;
  while(cin >> N >> M, N > 0){
	cin >> R;
	map<int,int> tmp[10001];
	vector<PII> log[10001];
	REP(i,R){
	  int t, n, m, s;
	  cin >> t >> n >> m >> s;
	  if(s == 0){
		log[m].PB(MP(tmp[m].find(n)->second, t));
		tmp[m].erase(n);
	  }
	  else
		tmp[m].insert(MP(n,t));	
	}
	
	REP(i,M+1) SORT(log[i]);
	int Q; cin >> Q;
	REP(q,Q){
	  int s, e, m; cin >> s >> e >> m;
	  int ans = 0, to;
	  REP(i,SZ(log[m])){
		if(e <= log[m][i].first)
		  break;
		if(log[m][i].second <= s) continue;
		if(s < log[m][i].first)
		  s = log[m][i].first;
		to = min(e, log[m][i].second);
		ans += to - s;
		s = log[m][i].second;
	  }
	  cout << ans << endl;
	}
  }
  return 0;
}