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 <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()
const int INF = 1e9;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

/* struct P {
	int x, y, n;
	P(int n, int x, int y):n(n), x(x), y(y){}
	P(){}
}; */


/** Problem1148 : Analyzing Login/Logout Records **/
bool login[10000][1300];

int main()
{
	int N, M;
	while (cin>>N>>M, N||M) {
		vector<int> login_num(M);
		vector<int> start(M);
		memset(login, 0, sizeof(login));
		
		int R; cin>>R;
		
		rep(i, R) {
			int t, n, m, s;
			cin>>t>>n>>m>>s;
			m--;
			
			if (s) {
				login_num[m]++;
				if (start[m]==0) start[m]=t;
			} else {
				login_num[m]--;
				
				if (login_num[m]==0) {
					for (int j=start[m]; j<t; j++) {
						login[m][j] = true;
					}
					
					start[m] = 0;
				}
				
			}
		}
		
		int Q; cin>>Q;
		
		rep(i, Q) {
			int s, e, m;
			cin>>s>>e>>m;
			m--;
			
			int ans=0;
			for (int j=s; j<e; j++) {
				if (login[m][j]) ans++;
			}
			
			cout << ans << endl;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long int i=0;i<n;++i)
typedef long long int ll;

int main(){

    while(1){
        int n,m;
        cin >> n >> m;
        if(n==0&&m==0)break;
        int r,q;
        cin >> r;
        vector<vector<int>> tt(m,vector<int>(1261,0));
        for(int i=0;i<r;i++){
            int t,pc,sid,s;
            cin >> t >> pc >> sid >> s;
            sid--;
            if(s==1){
                for(int j=t;j<1261;j++){
                    ++tt[sid][j];
                }
            }else{
                for(int j=t;j<1261;j++){
                    --tt[sid][j];
                }
            }
        }
        cin >> q;
        for(int i=0;i<q;i++){
            int s,e,sid;
            cin >> s >> e >> sid;
            sid--;
            int ans=0;
            for(int j=s;j<e;j++){
                if(tt[sid][j]>0){
                    ++ans;
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <complex>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 1010

bool solve(){
  int n,m,r,q;
  int rec_t[SIZE],rec_m[SIZE], rec_n[SIZE], rec_s[SIZE];
  int memo[SIZE],c[SIZE] = {};
  vector<pair<int,int> > vec[SIZE];
  
  scanf("%d%d%d",&n,&m,&r);

  if(n == 0) return false;
  
  for(int i=0;i<r;i++){
    scanf("%d%d%d%d",rec_t+i, rec_n+i, rec_m+i, rec_s+i);

    if(rec_s[i] == 1){
      if(c[rec_m[i]] == 0)
        memo[rec_m[i]] = rec_t[i];
      c[rec_m[i]]++;
    }else{
      c[rec_m[i]]--;

      if(c[rec_m[i]] == 0)
        vec[rec_m[i]].push_back({memo[rec_m[i]], rec_t[i]});
    }
  }

  scanf("%d",&q);

  for(int i=0;i<q;i++){
    int ts,te,m;
    int sum = 0;
    
    scanf("%d%d%d",&ts,&te,&m);

    for(int j=0;j<vec[m].size();j++){
      if(!(te <= vec[m][j].first || vec[m][j].second <= ts)){
        sum += min(te, vec[m][j].second) - max(ts, vec[m][j].first);
      }
    }

    printf("%d\n",sum);
  }

  return true;
}

int main(){

  while(solve());
  
  return 0;
}