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

using namespace std;

int main()
{
    while (true) {
        int n;
        int m;
        cin >> n >> m;
        if (n == 0 && m == 0) {
            break;
        }

        unordered_map<int, int> wip;
        using Range = tuple<int, int, int>;
        vector<Range> v;
        int r;
        cin >> r;
        for (int i = 0; i < r; ++i) {
            int t;
            int n_;
            int m_;
            int s;
            cin >> t >> n_ >> m_ >> s;
            if (s == 1) {
                wip[n_] = t;
            }
            else {
                v.push_back(make_tuple(m_, wip[n_], t));
                wip.erase(n_);
            }
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; ++i) {
            int t_s;
            int t_e;
            int m_;
            cin >> t_s >> t_e >> m_;
            auto merge = [](const Range& a, const Range& b) {
                int am, as, ae;
                int bm, bs, be;
                tie(am, as, ae) = a;
                tie(bm, bs, be) = b;
                int max_ = max(as, bs);
                int min_ = min(ae, be);
                if (max_ <= min_) {
                    make_pair(true, make_tuple(am, min(as, bs), max(ae, be)));
                }
                return make_pair(false, Range{});
            };
            vector<Range> cv;
            copy_if(begin(v), end(v), back_inserter(cv),
                    [m_](const Range& a) { return get<0>(a) == m_; });
            vector<Range> curr = cv;
            vector<Range> prev = cv;
            while (true) {
                prev = curr;
                curr = {};
                bool merged{false};
                for (int j = 0; j < static_cast<int>(prev.size()) - 1; ++j) {
                    auto a = merge(prev[j], prev[j + 1]);
                    if (a.first) {
                        curr.push_back(a.second);
                        curr.insert(end(curr), begin(prev) + j + 2, end(prev));
                        merged = true;
                        break;
                    }
                    curr.push_back(prev[j]);
                }
                if (!merged) {
                    break;
                }
            }
            // ??????????????????????????£???????????§?¨???????????????????prev?????¨??????????´?????????????????????§??????
            // curr????????????????´??????\??£??????????????????prev?????¨??????
            cv = prev;
            cv.erase(unique(begin(cv), end(cv)), end(cv));
            int c{0};
            for (auto&& j : cv) {
                int beg = max(t_s, get<1>(j));
                int en = min(t_e, get<2>(j));
                if (en - beg > 0) {
                    c += en - beg;
                }
            }
            cout << c << endl;
        }
    }
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int calc(int s, int e, int ts, int te) {
    int x = min(e, te) - max(s, ts);
    if(x < 0) x = 0; return x;
}

vector<pii> v[10010];

signed main() {
    int N, M;
    while(cin >> N >> M) {
        if(N == 0 && M == 0) break;
        int logs[10010][1010] = {};
        int r, t, n, m, s; cin >> r;
        rep(i,0,r) {
            cin >> t >> n >> m >> s;
            t -= 540;
            m--;
            if(s == 0) v[m].pb(pii(logs[m][n], t));
            else logs[m][n] = t;
        }

        int q; cin >> q;
        rep(i,0,q) {
            int ts, te, mm; cin >> ts >> te >> mm;
            ts -= 540; te -= 540;
            mm--;

            int ruiseki[1000] = {};
            rep(j,0,v[mm].size()) {
                ruiseki[ v[mm][j].fr     ]++;
                ruiseki[ v[mm][j].sc + 1 ]--;
            }
            int ans = 0;
            rep(j,1,1000) {
                ruiseki[j] += ruiseki[j-1];
                if(ruiseki[j] && ruiseki[j-1] && ts < j && j <= te) ans++;
            }
            cout << ans << endl;
        }
    }
    return 0;
}