#K37947. Warehouse Stock Management

    ID: 26089 Type: Default 1000ms 256MiB

Warehouse Stock Management

Warehouse Stock Management

You are given one or more datasets representing stock transactions in a warehouse. Each dataset begins with a line containing two integers \(n\) and \(t\), where \(n\) is the number of distinct items and \(t\) is the number of transactions to follow. Each of the next \(t\) lines contains four integers: the time of transaction \(t_i\) (an integer representing the minute of the day, with \(0 \leq t_i < 1440\)), the item identifier \(i\), the value \(v\) of stock changed, and an action flag \(a\) (where \(a=1\) signifies an addition, and \(a=0\) signifies a subtraction).

After the transactions, there is a line containing an integer \(q\) representing the number of queries. Each of the following \(q\) lines contains three integers: the start time \(ts\), the end time \(te\), and the item identifier \(i\). For each query, you need to compute the net change in stock for item \(i\) over the time period from \(ts\) to \(te\) (inclusive), using the formula:

[ \Delta = \sum_{t=ts}^{te} \Delta_{t}, ]

where \(\Delta_{t}\) is the net change applied at minute \(t\). The input terminates when a line "0 0" is encountered.

The output for each query should be printed on a separate line.

inputFormat

The input is read from standard input (stdin) and consists of one or more datasets. Each dataset has the following format:

n t
transaction_1
transaction_2
... (t transactions)
q
query_1
query_2
... (q queries)

Each transaction line has the format: time item value action.

Each query line has the format: start_time end_time item.

The end of input is indicated by a line with "0 0".

outputFormat

For each query in the order they appear, output the net stock change for the specified item, each on a separate line, to standard output (stdout).

## sample
3 5
60 1 10 1
120 2 20 0
180 1 5 0
240 3 15 1
300 2 5 1
3
0 240 1
60 300 2
0 300 3
0 0
5

-15 15

</p>