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 <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<pair<int, int> > VII;

int main() {
  
  const int TS = 530, TE = 1270;
  const int SPAN = TE-TS;
  int N, M;
  while(cin >> N >> M, N) {
    int r; cin >> r;
    VVI dat(M, VI(SPAN, 0));
    for(int i=0; i<r; i++) {
      int t, n, m, s; cin >> t >> n >> m >> s;
      m --; t -= TS;
      if(s) dat[m][t] ++;
      else dat[m][t] --;
    }
    
    for(int i=0; i<M; i++) {
      // imos PCs login: 1 logout: -1
      for(int j=0; j<SPAN; j++) {
	dat[i][j+1] += dat[i][j];
      }
    }
    
    VVI dat2(M, VI(SPAN, 0));
    
    for(int i=0; i<M; i++) {
      // 複数のPCをひとまとめにして、更に時間の累積和を取る
      for(int j=0; j<SPAN; j++) {
	dat2[i][j+1] = dat2[i][j] + (dat[i][j] > 0);
      }
    }
    
    int q;
    cin >> q;
    for(int i=0; i<q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m; m --; ts -= TS; te -= TS;
      cout << dat2[m][te] - dat2[m][ts] << endl;
    }
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int N, M, r, q;

int start = 540;
bool use[10010][1000];
int login[10010][1000];

int main() {
	while (1) {
		cin >> N >> M; if (N == 0 && M == 0) break;
		cin >> r;
		
		memset(use, 0, sizeof(use));
		memset(login, -1, sizeof(login));
		for (int i = 0; i < r; i++) {
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			n--; m--;
			if (s) {
				login[m][n] = t - start;
			} else {
				int in = login[m][n], out = t - start;
				for (int p = in; p < out; p++) use[m][p] = true;
			}
		}
		
		cin >> q;
		for (int i = 0; i < q; i++) {
			int ts, te, m; cin >> ts >> te >> m;
			ts -= start; te -= start; m--;
			int ans = 0;
			for (int p = ts; p < te; p++) if (use[m][p]) ans++;
			cout << ans << endl;
		}
	}
	
	return 0;
}

C++: 
#include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;
const int MAX_STUDENT = 10001;
const int MAX_TIME = 1261;
int main(){
  int N,M;
  while(true){
    cin >> N >> M;
    if(!N)break;
    int r;
    cin >> r;
    vector<vector<int>> timeline(MAX_STUDENT,vector<int>(MAX_TIME,0));
    REP(i,r){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s==0){
	timeline[m][t] -= 1;
      }else{
	timeline[m][t] += 1;
      }
    }
    REP(i,MAX_STUDENT)
      for(int j = 540;j<MAX_TIME;j++){
      timeline[i][j+1] += timeline[i][j];
    }
    int q;
    cin >> q;

    REP(i,q){
      int ct = 0;
      int t1,t2,m;
      cin >> t1 >> t2 >> m;
      for(int j = t1;j<t2;j++){
	if(timeline[m][j]>0)ct++;
      }
      cout << ct << endl;
    }
  }
  return 0;
}

C++: 



//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148&lang=jp




#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>




#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;




using namespace std;




int main()
{
	int N,M;




	while(cin >> N >> M && N)
	{
		vector<int> PC(N+1,0);
		int record[M+1][1261];
		fill_n((int *)record, sizeof(record) / sizeof(int), 0);




		int R;
		cin >> R;
		rep(nth,R)
		{
			int t,n,m,s;
			cin >> t >> n >> m >> s;
			if(s == 1){
				PC[n] = t;
			}else{
				int ts = PC[n];
				REP(i,ts,t){
					record[m][i] = 1;
				}
				PC[n] = 0;
			}
		}




		int Q;
		cin >> Q;
		rep(nth,Q)
		{
			int ts, te, m;
			cin >> ts >> te >> m;
			int ans=0;
			REP(i,ts,te){
				if(record[m][i]){ ans+=1; }
			}
			cout << ans << endl;
		}
	}




	return 0;
}

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

typedef vector<int> V;

typedef struct{
  int t,n,m,s;
} Record;

int main(){
  int n,m,r,q;
  
  while(cin >> n >> m ,n + m){
    const int MAX = 1260-540;

    cin >> r;
    Record d[r];
    vector<V> v(m,V(MAX+1,-1)),cnt(m,V(MAX+1,0));
    int use[1001] = {0};

    for(int i = 0 ; i < r ; i++){
      cin >> d[i].t >> d[i].n >> d[i].m >> d[i].s;
      if(d[i].s){
	use[d[i].n-1] = d[i].t;
      }
      else{
	for(int j = use[d[i].n-1] ; j < d[i].t ; j++){
	  cnt[d[i].m-1][j-540] = 1;
	}

      }
    }

    int ts,tt,sn;
    cin >> q;
    for(int i = 0 ; i < q ; i++){
      cin >> ts >> tt >> sn;

      sn--;
      bool flg = true;
      int ans = 0;
      for(int i = ts ; i < tt ; i++){
	if(cnt[sn][i-540] != 0){
	  ans++;
	}
      } 
      cout << ans << endl;
    }
  }

  return 0;
}