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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define SQ(x) ((x)*(x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1<<29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;



int re[10001][1300];
int N,M;


int main(){
  while(1){
    memset(re,0,sizeof(re));
    int r,q,t,n,m,s,ts,te;
    scanf("%d%d",&N,&M);
    if(N+M==0) break;
    scanf("%d",&r);
    rep(i,r){
      scanf("%d%d%d%d",&t,&n,&m,&s); m--;
      if(s) re[m][t] = 1;
      else re[m][t] = -1;
    }

    rep(i,10000){
      rep(j,1260) re[i][j+1] += re[i][j];
    }

    scanf("%d",&q);
    rep(i,q){
      scanf("%d%d%d",&ts,&te,&m); m--;
      int res = 0;
      for(int j=ts;j<te;j++) if(re[m][j]>0) res++;
      //      for(int j=ts;j<te;j++) cout << re[m][j] << " "; cout << endl;
      printf("%d\n", res);
    }
  }
  return 0;
}

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

struct record {
  int t, n, s;
};

int main() {
  int N, M;
  while (cin >> N >> M and N) {
    int r;
    cin >> r;
    vector<int> ts(r), ns(r), ms(r), ss(r);
    for (int i = 0; i < r; i++) {
      cin >> ts[i] >> ns[i] >> ms[i] >> ss[i];
      ms[i]--, ns[i]--;
    }

    vector< vector<record> > students(M);
    for (int i = 0; i < r; i++) {
      record rd;
      rd.t = ts[i], rd.n = ns[i], rd.s = ss[i];
      students[ms[i]].push_back(rd);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;
      
      vector<int> times(1270);
      for (int i = 0; i < 1270; i++) {
        times[i] = 0;
      }
      vector<record> records = students[m];
      for (record rd : records) {
        if (rd.s == 1) {
          times[rd.t]++;
        } else if (rd.s == 0) {
          times[rd.t]--;
        }
      }

      for (int i = 1; i < 1270; i++) {
        times[i] += times[i-1];
      }

      int ans = 0;
      for (int i = ts; i < te; i++) {
        if (times[i] > 0) {
          ans++;
        }
      }

      cout << ans << endl;
    }
  }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>

int main()
{
	while(true){
		int N, M;

		std::cin >> N >> M;
		if ( N == 0 && M == 0 ) break;

		int r;
		std::cin >> r;

		std::vector<std::tuple<int, int, int>> data(r);
		for ( int i = 0; i < r; ++i ) {
			auto &v = data[i];

			int n;
			std::cin >> std::get<0>(data[i]);
			std::cin >> n;
			std::cin >> std::get<1>(data[i]);
			std::cin >> std::get<2>(data[i]);
		}

		std::sort(data.begin(), data.end());
		int q;
		std::cin >> q;
		for ( int i = 0; i < q; ++i ) {
			int s, e, m;
			std::cin >> s >> e >> m;

			int be = s;
			int sum = 0;
			int pc = 0;
			for ( int j = 0; j < r; ++j ) {
				if ( std::get<1>(data[j]) == m ) {
					if ( std::get<2>(data[j]) == 1 ) {
						pc++;
					}else {
						pc--;
					}

					if ( std::get<0>(data[j]) >= e && pc == 1 ) break;
					if ( be < std::get<0>(data[j]) && std::get<2>(data[j]) == 1 ) {
						if ( pc == 1 )
							be = std::get<0>(data[j]);
					}
					if ( std::get<2>(data[j]) == 0 ) {
						if ( e > std::get<0>(data[j]) ) {
							if ( pc == 0 )
								sum += std::get<0>(data[j]) - be;
						} else {
							sum += e - be;
							break;
						}
					}
				}
			}
			std::cout << sum << std::endl;
		}
	}
	
	return 0;
}

C++: // Undone
#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
#define MOD 1000000007
using namespace std;
int student[100100][2000];
stack<int> login[1010];
signed main() {
  int N, M, r, q;
  while (cin >> N >> M, N+M) {
    // initialize
    for (int i = 0; i < M; i++) {
      for (int j = 540; j < 1261; j++) {
        student[i][j] = 0;
      }
    }

    // input
    cin >> r;
    for (int i = 0; i < r; i++) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      if (s) {
        // login
        login[n].push(t);
      } else {
        // logout
        for (int j = login[n].top(); j < t; j++) {
          student[m][j] = 1;
        }
        login[n].pop();
      }
    }
    
    // output
    cin >> q;
    for (int i = 0; i < q; i++) {
      int t, u, m;
      cin >> t >> u >> m;
      int ans = 0;
      for (int j = t; j < u; j++) {
        ans += student[m][j];
      }
      cout << ans << endl;
    }
  }  
}