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;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 24;

int main(void){
    for(int n, m; cin >> n >> m, n;){
        int r; cin >> r;

        vector<vector<pii>> table(m);
        vi cnt(m);
        rep(aaa, r){
            int t, nn, mm, s; cin >> t >> nn >> mm >> s;
            nn--; mm--;

#define F first
#define S second

            if(table[mm].size() == 0 || table[mm][table[mm].size() - 1].S != inf){
                cnt[mm]++;
                table[mm].pb(mp(t, inf));
            }
            else {
                if(s == 1){
                    cnt[mm]++;
                }
                else {
                    cnt[mm]--;
                    if(cnt[mm] == 0){
                        table[mm][table[mm].size() - 1].S = t;
                    }
                }
            }
        }

        int q; cin >> q;
        rep(aaa, q){
            int ts, te, mm; cin >> ts >> te >> mm;
            mm--;

            int res = 0;

            for(auto && tt : table[mm]){
                int s, e;
                s = max(ts, tt.F);
                e = min(te, tt.S);

                if(e - s > 0) res += (e - s);
            }

            cout << res << endl;
        }
    }

	return 0;
}

C++: #include <iostream>
#include <map>

using namespace std;

class State{
public:
  int name;
  int st;
};

main(){
  int N, M, r, q, t, n, m, s;
  
  bool data[721][10000];


  while(1){
    cin >> N >> M;
    map<int,State> in;
    if(N==0 && M==0) break;
    for(int i=0;i<721;i++){
      for(int j=0;j<M;j++){
	data[i][j]=false;
      }
    }

    cin >> r;
    for(int i=0;i<r;i++){
      cin >> t >> n >> m >> s;
      m--;
      if(s==1){
	in[n].name=m;
	in[n].st=t-540;
      }else if(s==0){
	for(int i=in[n].st;i<(t-540);i++){
	  data[i][m]=true;
	}
      }
    }

    cin >> q;
    for(int i=0;i<q;i++){
      int ts, te;
      cin >> ts >> te >> m;
      m--;
      int count=0;
      for(int i=ts-540;i<(te-540);i++){
	if(data[i][m]) count++;
      }
      cout << count << endl;
    }
  }
  return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k;
  int t[1000],o[1000],p[1000],s[1000],c[50];
  int m,n,r,q,b,e,l,u;
  int sz;
  vector<int> z;
  while(cin>>n>>m&&n+m){
    cin>>r;
    for(i=0;i<r;i++)
      cin>>t[i]>>o[i]>>p[i]>>s[i];
    cin>>q;
    for(i=0;i<q;i++){
      cin>>b>>e>>l;
      z.clear();
      u=0;
      for(j=0;j<r;j++){
	if(p[j]==l){
	  if(s[j]){
	    if(u==0)
	      z.push_back(t[j]);
	    u++;
	  }else{
	    u--;
	    if(u==0)
	      z.push_back(t[j]);
	  }
	}
      }
      sz=z.size();
      if(sz==0){
	c[i]=0;
      }else{
	if(b<z[0]){
	  j=-1;
	}else{
	  for(j=0;j<sz;j+=2){
	    if(z[j]<=b&&b<=z[j+1])
	      break;
	  }
	}
	if(e<z[0]){
	  k=-1;
	}else{
	  for(k=0;k<sz;k+=2){
	    if(z[k]<=e&&e<=z[k+1])
	      break;
	  }
	}
	if(0){
	}else if(j==sz||k==-1){
	  c[i]=0;
	}else if(j==k){
	  c[i]=e-b;
	}else if(j==-1&&k==sz){
	  c[i]=0;
	  for(j=0;j<sz;j+=2)
	    c[i]+=z[j+1]-z[j];
	}else if(j==-1){
	  c[i]=e-z[k];
	  for(j=0;j<k;j+=2)
	    c[i]+=z[j+1]-z[j];
	}else if(k==sz){
	  c[i]=z[j+1]-b;
	  for(j+=2;j<sz;j+=2)
	    c[i]+=z[j+1]-z[j];
	}else{
	  c[i]=z[j+1]-b;
	  c[i]+=e-z[k];
	  for(j+=2;j<k;j+=2)
	    c[i]+=z[j+1]-z[j];
	}
      }
      cout<<c[i]<<endl;
    }
  }
  return 0;
}