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>
#include <stdint.h>

typedef uint32_t u32;
typedef uint64_t u64;
typedef std::pair<int, int> pii;

class Data {
public:
  Data() {}
  Data(int t, int n, int m, int s)
    : t(t), n(n), m(m), s(s)
  {}
  int t, n, m, s;
};

bool operator < (const Data& left, const Data& right) {
  return left.t < right.t;
}

Data data[10000];

int main() {

  for(;;) {
    int N, M;
    std::cin >> N >> M;
    if( N == 0 and M == 0 ) break;
    int r;
    std::cin >> r;
    for(int i = 0; i < r; ++i) {
      int t, n, m, s;
      std::cin >> t >> n >> m >> s;
      data[i] = Data(t, n, m, s);
    }
    std::sort(data, data + r);
    int q;
    std::cin >> q;
    for(int i = 0; i < q; ++i) {
      int t1, t2, m;
      std::cin >> t1 >> t2 >> m;
      int res = 0;
      int use[2000] = {};
      for(int j = 0; j < r; ++j) {
        if( m != data[j].m ) continue;
        if( data[j].s == 1 ) {
          use[data[j].t] += 1;
        }
        else {
          use[data[j].t] -= 1;
        }
      }
      int t = 0;
      for(int j = 0; j < 2000; ++j) {
        t += use[j];
        use[j] = t;
      }
      for(int j = t1; j < t2; ++j) {
        if( use[j] != 0 ) {
          res += 1;
        }
      }
      std::cout << res << std::endl;        
    }
  }
    
  
  return 0;
}

C++: #include <iostream>
#include <utility>
#include <string>
using namespace std;

int main(void){
  while(1){
    int N,M,r,q;
    cin >> N;
    cin >> M;
    if(N == 0){return 0;}

    cin >> r;

    int t[r];
    int n[r];
    int m[r];
    int s[r];
    for(int i = 0 ; i < r; i++){
      cin >> t[i];
      cin >> n[i];
      cin >> m[i];
      cin >> s[i];
    }

    cin >> q;
    int ts[q];
    int te[q];
    int qm[q];
    for(int i = 0 ; i < q ; i++){
      cin >> ts[i];
      cin >> te[i];
      cin >> qm[i];
    }

    for(int qnum = 0 ; qnum < q ; qnum++){
      int ans = 0;

      bool login[1261];
      for(int ptr = 0 ; ptr < 1261 ; ptr++){
        login[ptr] = false;
      }

      int  minstart;
      int  usepc;
      int  minend;
      for(int i = 0 ; i < r ; i++){

        if(m[i] == qm[qnum] && s[i] == 1){
          minstart = t[i];

          bool flg = true;

          for(int j = i+1 ; j <= r && flg ; j++){
            if(n[i] == n[j] && m[i] == m[j] && s[j] == 0){
              flg = false;
              minend = t[j];
            }
          }

          for(int ptr = minstart ; ptr < minend ; ptr++){
            login[ptr] = true;
          }
//          cout << minstart << "??????" << minend << "?????§" << endl;
        }

      }

      for(int ptr = ts[qnum] ; ptr < te[qnum] ; ptr++){
        if(login[ptr]){
//          cout << ptr << "????????£?????????" << endl;
          ans++;
        }
      }
      cout << ans << 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;
	map<int,int> tmp[10001];
	vector<PII> log[10001];
	REP(i,R){
	  int t, n, m, s;
	  cin >> t >> n >> m >> s;
	  auto it = tmp[m].find(n);
	  if(it != tmp[m].end()){
		log[m].PB(MP(it->second, t));
		tmp[m].erase(it);
	  }
	  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;
}