#include <stdio.h>
#define ll long long
#define rep(i,l,r)for(ll i=l;i<r;i++)

ll time[10010];
ll hito[10010];
ll st[10010];

int main(){
	ll z;
	while(scanf("%lld%*lld",&z),z){
		ll n;
		scanf("%lld",&n);
		rep(i,0,n)scanf("%lld%*lld%lld%lld",time+i,hito+i,st+i);
		ll m;
		scanf("%lld",&m);
		rep(i,0,m){
			ll start,end,hi;
			scanf("%lld%lld%lld",&start,&end,&hi);
			ll ans=0,cnt=0,pos=0;
			rep(t,540,1260){
				while(pos<n&&time[pos]<=t){
					if(hito[pos]==hi){
						if(st[pos]==1)cnt++;
						else cnt--;
					}
					pos++;
				}
				if(start<=t&&t<end)ans+=!!cnt;
			}
			printf("%lld\n",ans);
		}
	}
}
