extern crate core;

use std::str::FromStr;
use std::fmt;
use std::cmp::{Ordering, max};
use std::fmt::{Display, Error, Formatter};
use std::ops::{Range, Add};

struct InputStream {
    line: Vec<String>,
    position: usize
}
type Parse<T: FromStr> = Result<T, T::Err>;
impl InputStream {
    fn new() -> InputStream {
        InputStream {line:vec![], position: 0}
    }
    fn read_word(&mut self) -> &String {
        if self.position == self.line.len() {
            let mut line= String::new();
            std::io::stdin().read_line(&mut line).ok();
            self.line = line.split_whitespace().map(|x|x.to_string()).collect();
            self.position = 0
        }
        let result = &self.line[self.position];
        self.position += 1;
        result
    }
    fn read<T: FromStr>(&mut self) -> Parse<T> {
        self.read_word().parse()
    }
    fn get<T: FromStr>(&mut self) -> T {self.read().ok().unwrap()}
    fn read_int(&mut self) -> i32 {self.get()}
    fn read_long(&mut self) -> i64 {self.get()}
    fn read_double(&mut self) -> f64 {self.get()}
    fn read_usize(&mut self) -> usize {self.get()}
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}

macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! repeat {
    ($count:expr; $($statement:tt)+) => {
        for _ in 0 .. $count {
            $($statement)*
        }
    };
}

macro_rules! yields {
    ($count:expr; $block:block) => {{
        let mut vec = Vec::with_capacity($count);
        for _ in 0 .. $count {
            vec.push($block);
        }
        vec
    }}
}
macro_rules! array {
    ($factor: expr; ) => {$factor};
    ($factor: expr; $dimension: expr) => {{
        let size = $dimension;
        let mut vec = Vec::with_capacity(size);
        for _ in 0 .. size {
            vec.push($factor)
        }
        vec
    }};
    ($factor:expr; $dimension:expr, $($r:expr),*) => {{
        let size = $dimension;
        let mut vec = Vec::with_capacity(size);
        for _ in 0 .. size{
            let factor = array!($factor; $($r),*);
            vec.push(factor)
        }
        vec
    }};
}
fn tabulate<F, R>(count: usize, mut generator: F) -> Vec<R> where F : FnMut(usize) -> R {
    let mut result = Vec::<R>::with_capacity(count);
    for f in 0..count {
        result.push(generator(f));
    }
    result
}
fn main() {
    let mut cin = InputStream::new();
    loop {
        let n = cin.read_usize();let m = cin.read_usize();
        if n == 0 && m == 0 {
            return;
        }
        let r = cin.read_usize();
        let mut time_table:Vec<Vec<i32>> = vec![vec![0; 0]; m];
        let mut is_calculated = vec![false; m];
        for _ in 0 .. r {
            let t = cin.read_usize();let n = cin.read_usize();let m = cin.read_usize();let s = cin.read_usize();
            if s == 1 {
                if time_table[m - 1].is_empty() {
                    time_table[m - 1] = vec![0; 721];
                }
                time_table[m - 1][t - 540] += 1;
            }else {
                time_table[m - 1][t - 540] -= 1;
            }
        }
        let q = cin.read_usize();
        for _ in 0 .. q {
            let ts = cin.read_usize(); let te = cin.read_usize(); let m = cin.read_usize();
            if !is_calculated[m - 1] {
                for t in 1..time_table[m - 1].len() {
                    time_table[m - 1][t] += time_table[m - 1][t - 1];
                }
                for t in 1..time_table[m - 1].len() {
                    if time_table[m - 1][t] > 1 {
                        time_table[m - 1][t] = 1;
                    }
                    time_table[m - 1][t] += time_table[m - 1][t - 1];
                }
                is_calculated[m - 1] = true;
            }
            let mut time = 0;
            if ts >= 541 && time_table[m - 1].len() > ts - 541 {
                time -= time_table[m - 1][ts - 541];
            }
            if time_table[m - 1].len() > 0 {
                time += time_table[m - 1][te - 541];
            }
            println!("{}", time);
        }
    }
}



Measured memory_bytes: 5040