#include <iostream>
#include <vector>
#include <bitset>

using namespace std;

void solve()
{
	int N, M;
	while(cin >> N >> M, N || M)
	{
		vector<bitset<721> > used(M + 1);
		vector< vector<int> > login(M + 1, vector<int>(N + 1));
		int r;
		cin >> r;
		for(int i = 0; i < r; ++i)
		{
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if(s == 0)
			{
				for(int j = login[m][n]; j < t - 540; ++j)
				{
					used[m].set(j);
				}
			}
			else
			{
				login[m][n] = t - 540;
			}
		}
		int q;
		cin >> q;
		for(int i = 0; i < q; ++i)
		{
			int ts, te, m;
			cin >> ts >> te >> m;
			int count = 0;
			for(int j = ts - 540; j < te - 540; ++j)
			{
				if(used[m][j])
				{
					++count;
				}
			}
			cout << count << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}