Analyzing Login/Logout Records

No attempts yetTime limit1sMemory limit128 MB

Problem

A university runs a computer literacy course. In its computer system, the login/logout records of every PC for one day are stored in a file. A student may use two or more PCs at the same time, but nobody can log in to a PC that another student has logged in to and not yet logged out of.

Write a program that, for a given time period (for example, a laboratory class), computes the total time during which a student was using at least one PC, based on the records in the file.

The following is an example of login/logout records.

  • Student 1 logged in to PC 1 at 12:55
  • Student 2 logged in to PC 4 at 13:00
  • Student 1 logged in to PC 2 at 13:10
  • Student 1 logged out of PC 2 at 13:20
  • Student 1 logged in to PC 3 at 13:30
  • Student 1 logged out of PC 1 at 13:40
  • Student 1 logged out of PC 3 at 13:45
  • Student 1 logged in to PC 1 at 14:20
  • Student 2 logged out of PC 4 at 14:30
  • Student 1 logged out of PC 1 at 14:40

For a query such as "Give the usage of student 1 between 13:00 and 14:30", the program should answer "55 minutes": the sum of 45 minutes from 13:00 to 13:45 and 10 minutes from 14:20 to 14:30.

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space (0 0). The number of datasets never exceeds 10.

Each dataset has the following format.

N M
r
record1
...
recordr
q
query1
...
queryq

On the first line, $N$ and $M$ are the number of PCs and the number of students, respectively. $r$ is the number of records and $q$ is the number of queries. These four integers satisfy:

  • $1 \le N \le 1000$, $1 \le M \le 10000$, $2 \le r \le 1000$, $1 \le q \le 50$

Each record consists of four space-separated integers $t\ n\ m\ s$.

$s$ is 0 or 1. If $s$ is 1, the line means that student $m$ logged in to PC $n$ at time $t$; if $s$ is 0, it means that student $m$ logged out of PC $n$ at time $t$. Time is expressed as the number of minutes elapsed since 0:00 of the day. $t$, $n$, and $m$ satisfy:

  • $540 \le t \le 1260$, $1 \le n \le N$, $1 \le m \le M$

You may assume the following about the records.

  • Records are stored in ascending order of time $t$.
  • No two records for the same PC have the same time $t$.
  • No PC is logged in before the time of the first record or after the time of the last record in the file.
  • Login and logout records for one PC appear alternately, and each login-logout pair is for the same student.

Each query consists of three space-separated integers $t_s\ t_e\ m$.

It represents "the usage of student $m$ between $t_s$ and $t_e$". $t_s$, $t_e$, and $m$ satisfy:

  • $540 \le t_s < t_e \le 1260$, $1 \le m \le M$

Output

For each query, print one line containing a decimal integer: the usage time in minutes. Output lines must not contain any character other than this number.