#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int N,M,p,t,n,m,s;
	while(cin >> N >> M, N||M)
	{
		vector<short> tim,num,swt;

		cin >> N;
		while(N--)
		{
			cin >> t >> n >> m >> s;
			tim.push_back(t);
			num.push_back(m);
			swt.push_back(s);
		}

		cin >> N;
		while(N--)
		{
			int lg[1024] = {0};

			cin >> t >> s >> m;		
			for(int i=0; i<num.size(); i++)
			{
				if(m==num[i])
				{
					int d = swt[i]*2-1;
					for(int j=tim[i]-540; j<1024; j++)
					{
						lg[j]+=d;
					}
				}
			}


			int a=0;
			for(int i=t-540; i<s-540; i++)
			{
				if(lg[i]>0) a++;
			}
			cout << a << endl;
		}
		
	}
}