import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

const real eps = 1e-10;

void main(){
	while(true) {
		auto nm = readInts();
		auto N = nm[0];
		auto M = nm[1];
		if((N|M) == 0) return;

		auto r = readInt();
		auto login = new int[][](M, 721);
		for(int i; i < r; ++i) {
			auto tnms = readInts();
			auto t = tnms[0]-540;
			auto n = tnms[1];
			auto m = tnms[2]-1;
			auto s = tnms[3];
			if(s == 1) {
				++login[m][t];
			} else {
				--login[m][t];
			}
		}
		for(int i; i < M; ++i) {
			for(int j = 1; j < login[i].length; ++j) {
				login[i][j] += login[i][j-1]; 
			}
		}
		auto q = readInt();
		for(int i; i < q; ++i) {
			auto sem = readInts();
			auto ts = sem[0]-540;
			auto te = sem[1]-540;
			auto m = sem[2]-1;
			writeln(login[m][ts..te].filter!(a => a > 0).array.length);
		}
	}
}