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

using namespace std;

int N, M, r, q;
bool use[1261] = {false};

class Log {
private:
    int t_;
    int n_;
    int m_;
    int s_;
public:
    Log(int t, int n, int m, int s) :
        t_(t), n_(n), m_(m), s_(s) {}

    int t() {
        return t_;
    }

    int n() {
        return n_;
    }

    int m() {
        return m_;
    }

    int s() {
        return s_;
    }
};

class Question {
private:
    int ts_;
    int te_;
    int m_;
public:
    Question(int ts, int te, int m) :
        ts_(ts), te_(te), m_(m) {}
};

int solve(vector<Log>& logs, int ts, int te, int m) {
    int ans = 0;
    vector<int> login;
    vector<int> logout;

    for (int i = 0; i < 1261; ++i) {
        use[i] = false;
    }

    for (int i = 1; i < N + 1; ++i) {
        for (auto& e : logs) {
            if (e.m() == m && e.s() == 0 && e.n() == i) {
                logout.push_back(e.t());
            }
            else if (e.m() == m && e.s() == 1 && e.n() == i) {
                login.push_back(e.t());
            }
        }
    }

    const int size = login.size();

    // for (int i = 0; i < size; ++i) {
    //     cout<<login[i]<<" - "<<logout[i]<<endl;
    // }
    

    for (int i = 0; i < size; ++i) {
        for (int j = login[i]; j < logout[i]; ++j) {
            use[j] = true;
        }
    }

    for (int time = 540; time <= 1260; ++time) {
        if  (use[time] && ts <= time && time < te) {
            ans++;
            //cout<<"time : "<<time<<", ans : "<<ans<<endl;
        }
    }

    return ans;
}

int main() {
    int t, n, m, s;
    int ts, te;
    vector<Log> log;

    while(cin>>N>>M, N||M) {
        cin>>r;
        for (int i = 0; i < r; ++i) {
            cin>>t>>n>>m>>s;
            log.push_back(Log(t, n, m, s));
        }

        cin>>q;
        for (int i = 0; i < q; ++i) {
            cin>>ts>>te>>m;
            cout<<solve(log, ts, te, m)<<endl;
        }
        
        log.clear();
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<set>
#include<functional>
#include<map>
#define MAX 10010
#include<queue>
using namespace std;
typedef pair<int,int> P;
int main(){
	int e,q,t,n,m,s,p;
	while(cin>>e>>q,e||q){
		vector<P> tab[MAX];
		cin>>p;
		for(int i=0;i<p;i++){
			cin>>t>>n>>m>>s;
			tab[m-1].push_back(P(t,s));
		}
		int now,t1,t2;
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>t1>>t2>>m;
			now=t1;
			int ans=0,sum=0;
			int j;
			m--;
			for(j=0;j<tab[m].size()&&tab[m][j].first<t2;j++){
				if(!tab[m][j].second){
					if(sum>0&&tab[m][j].first>=t1&&tab[m][j].first<t2){
						ans+=tab[m][j].first-now;
					}sum--;
					if(tab[m][j].first>t1&&tab[m][j].first<t2)
					now=tab[m][j].first;
				}
				else if(tab[m][j].second){
					if(sum>0&&tab[m][j].first>t1&&tab[m][j].first<t2){
						ans+=tab[m][j].first-now;
					}sum++;
					if(tab[m][j].first>=t1&&tab[m][j].first<t2)
					now=tab[m][j].first;
				}
			}
			if(sum>0)
			ans+=t2-now;
			cout<<ans<<endl;
			
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <array>
#include <cmemory>

using namespace std;

#define all(c) begin(c), end(c)
#define dump(c) cerr << "> " << #c << " = " << (c) << endl
#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)

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

const int INF = 1001001001;
const double EPS = 1e-10;


template<typename T1,typename T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p){
  return os << '(' << p.first <<',' << p.second << ')';
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a){
  os << '[';
  rep(i,a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int cnt[10010], st[10010], acc[10010][1300];

int main() {
  for(int N, M; cin >> N >> M, (N | M) != 0; ) {
    memset(cnt, 0, sizeof(cnt));
    memset(st, 0, sizeof(st));
    memset(acc, 0, sizeof(acc));
    int r; cin >> r;
    rep(i, r) {
      int t, n, m, s; cin >> t >> n >> m >> s;
      if (s) {
        if(cnt[m] == 0) st[m] = t;
        cnt[m]++;
      }
      else {
        cnt[m]--;
        if(cnt[m] == 0) {
          REP(j, st[m], t) acc[m][j] = 1;
        }
      }
    }

    int q; cin >> q;
    rep(i, q) {
      int s, e, m; cin >> s >> e >> m;
      int cnt = 0;
      REP(i, s, e) cnt += acc[m][i];
      cout << cnt << endl;
    }
  }
  
  
  return 0;
}