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>
#define MAX_T 1261
#define MAX_M 10001
#define MAX_R 1000
using namespace std;

int imos[MAX_M][MAX_T];
int rec[MAX_R][4];

void answer(int s, int t, int student) {
  int count = 0;
  for (int i = s; i < t; i++) {
    if (imos[student][i]) count++;
  }
  cout << count << endl;
}

void search(int n, int m) {
  for (int i = 0; i < n; i++) {
    if (rec[i][3] == 0) continue;
    imos[rec[i][2]][rec[i][0]]++;
    for (int j = i + 1; j < n; j++) {
      if (rec[j][1] == rec[i][1] && rec[j][3] == 0) {
	imos[rec[j][2]][rec[j][0]]--;
	break;
      }
    }
  }

  for (int i = 0; i <= m; i++) {
    for (int j = 500; j < MAX_T; j++) {
      imos[i][j] += imos[i][j - 1];
    }
  }
}

void init() {
  for (int i = 0; i < MAX_M; i++) {
    for (int j = 0; j < MAX_T; j++) {
      imos[i][j] = 0;
    }
  }
  for (int i = 0; i < MAX_R; i++) {
    for (int j = 0; j < 4; j++) {
      rec[i][j] = 0;
    }
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;
    init();
    int r;
    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> rec[i][0] >> rec[i][1] >> rec[i][2] >> rec[i][3];
    }

    search(r, m);

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m;
      answer(ts, te, m);
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) {
        ll d = euclid(b, a % b, y, x);
        return y -= a / b * x, d;
    }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1)
            ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1);
        return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e)
            return Mod(1);
        Mod r = *this ^ (e / 2);
        r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int isprime(int n) {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0 && (i % 7 == 1 || i % 7 == 6) && (n / i % 7 == 1 || n / i % 7 == 6)) {
            return false;
        }
    }
    return true;
}

int toint(string s) {
    string b;
    copy_if(all(s), back_inserter(b), [](char c) { return c != ':'; });
    return stoi(b);
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    for (;;) {
        int N, M, r, q;
        cin >> N >> M;

        if (!N) break;

        cin >> r;
        
        map<int, map<int, map<int, int>>> rec;

        rep(i, 0, r) {
            int t, n, m, s; cin >> t >> n >> m >> s;
            rec[m][n][t] = s;
        }

        vector<vi> u(M + 1, vi(1261, 0)), ups(M + 1, vi(1261, 0));
        for (auto p1 : rec) {
            int stud = p1.first;
            for (auto p2 : p1.second) {
                for (auto it = p2.second.begin(); it != p2.second.end(); ++it) if (it->second) {
                    // cout << "Stud " << stud << "; PC " << p2.first << "; Time " << it->first << "; Event " << it->second << endl;
                    int lo = it->first, hi = next(it) == p2.second.end() ? 1260 : next(it)->first;
                    // cout << "OR " << lo << ", " << hi << ", " << it->second << endl;
                    for (int i = lo; i < hi; i++) u[stud][i] |= it->second;
                }
            }
        }

        rep(i, 1, M + 1) partial_sum(all(u[i]), ups[i].begin());

        cin >> q;

        rep(i, 0, q) {
            int s, t, e; cin >> s >> t >> e;
            // cout << "Query User " << e << "; " << s  << " ~ " << t << endl;
            cout << (ups[e][t - 1] - ups[e][s - 1]) << endl;
        }
        // cout << "---" << endl;
    }
}