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 pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  while(1) {
    int n, m; cin >> n >> m;
    if(n == 0 && m == 0) break;
    vector<vector<vector<P>>> a(m);
    vector<vector<P>> b(m);
    vector<vector<P>> c(m);
    REP(i, m) {
      a[i].resize(n);
    }

    int r; cin >> r;
    REP(i, r) {
      int nowt, nown, nowm, nows; cin >> nowt >> nown >> nowm >> nows;
      nown--; nowm--;
      if(nows == 1) {
        a[nowm][nown].pb(P(nowt, 0));
      } else {
        a[nowm][nown][a[nowm][nown].size()-1].se = nowt;
      }
    }

    REP(i, m) {
      REP(j, n) {
        REP(k, SZ(a[i][j])) {
          b[i].pb(a[i][j][k]);
        }
      }
    }


    REP(i, m) {
      // cout << "b" << ":" << SZ(b[i]) << endl;
      if(SZ(b[i])) {
        sort(ALL(b[i]));
        c[i].pb(b[i][0]);
      }

      FOR(j, 1, SZ(b[i])) {
        // cout << i << ":" << j << endl;
        if(c[i][SZ(c[i])-1].se <= b[i][j].fi) {
          c[i].pb(b[i][j]);
        } else {
          c[i][SZ(c[i])-1].se = max(c[i][SZ(c[i])-1].se, b[i][j].se);
        }
      }
      // cout << "c:" << SZ(c[i]) << endl;
    }

    // REP(i, m) {
    //   REP(j, SZ(c[i])) {
    //     // cout << i << ":" << j << ":" << c[i][j].fi << ":" << c[i][j].se << endl;
    //   }
    // }

    int q; cin >> q;
    REP(i, q) {
      int ts, te, nowm; cin >> ts >> te >> nowm;
      nowm--;
      int ans = 0;
      REP(j, SZ(c[nowm])) {
        int tmp = min(te, c[nowm][j].se) - max(ts, c[nowm][j].fi);
        if(tmp > 0) ans += tmp;
      }
      cout << ans << endl;
    }
    
  }
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    while(true){
        int N,M;
        cin>>N>>M;
        if(N==0)break;
        int r;
        cin>>r;
        vector<int> t(r),n(r),m(r),s(r);
        for(int i=0;i<r;i++){
            cin>>t[i]>>n[i]>>m[i]>>s[i];
        }
        int q;
        cin>>q;
        vector<int> ts(q),te(q),qm(q);
        for(int i=0;i<q;i++){
            cin>>ts[i]>>te[i]>>qm[i];
        }
        for(int i=0;i<q;i++){
            int sum=0;
            int start=0;
            for(int j=0;j<r;j++){
                if(m[j]==qm[i]){
                    if(s[j]==1){
                        start=t[j];
                    }else{
                        sum+=min(t[j],te[i])-max(ts[i],start);
                    }
                }
            }
            cout<<sum<<endl;
        }
    }
}

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;
	int t[1001], n[1001], m[1001], s[1001];
	REP(i,R)
	  cin >> t[i] >> n[i] >> m[i] >> s[i];
	
	int Q; cin >> Q;
	REP(q,Q){
	  int st, et, x; cin >> st >> et >> x;
	  int sum = 0, ans = 0, prv = 0;
	  REP(i,R){
		if(m[i] == x){
		  if(sum == 0){
			prv = max(st, t[i]);
			++sum;
		  }
		  else if(sum > 0){
			ans += min(et, t[i]) - prv;
			if(s[i]) ++sum;
			else --sum;
			prv = t[i];
		  }
		}
	  }
	  cout << ans << endl;
	}
  }
  return 0;
}