#P8548. 小挖的买花

小挖的买花

Background

Xiao Wa likes buying flowers, but they are too lazy. So this task is completely handed over to you.

Problem Description

There are only nn flowers in the shop. Each flower has three attributes: price costicost_i, beauty beibe_i, and freshness frifr_i.

Xiao Wa has different requirements each time. More precisely, for the jj-th purchase, the money you have can buy flowers with total cost at most cjc_j. At the same time, Xiao Wa also requires that the total freshness is at least fjf_j. Xiao Wa wants to know: after meeting these conditions, what is the maximum possible total beauty of the flowers you buy? If no matter what you do, you cannot make the total freshness at least fjf_j, output 00.

Xiao Wa will ask you to buy flowers a total of qq times. Can you answer their questions correctly? The queries are independent of each other.

Input Format

The first line contains two positive integers n,qn, q.

Lines 2n+12 \sim n + 1 each contain three positive integers costi,fri,beicost_i, fr_i, be_i, representing the three attributes of a flower.

Lines n+2n+q+1n + 2 \sim n + q + 1 each contain two positive integers cj,fjc_j, f_j, representing the requirements for each purchase.

Output Format

Output qq lines. Each line contains one integer, the maximum total beauty. If there is no solution, output 00.

5 1
2 4 5
4 3 3
1 3 2
3 4 3
3 2 5
10 10

15

Hint

For 20%20\% of the testdata, 3n,q163\leq n, q\leq 16.

For 40%40\% of the testdata, 3n,q303\leq n, q\leq 30, 0cj,fj500\leq c_j, f_j\leq 50.

For 60%60\% of the testdata, 3n1003\leq n\leq 100, 1q5×1041\leq q\leq 5\times 10^4, 0costi,fri,cj,fj1000\leq cost_i, fr_i, c_j, f_j\leq 100.

For the other 20%20\% of the testdata, for each purchase, fj=0f_j = 0.

For 100%100\% of the testdata, 3n5003\leq n\leq 500, 1q106\boldsymbol{1\leq q\leq 10^6}, 0costi,fri,cj,fj5000\leq cost_i, fr_i, c_j, f_j\leq 500, 1bei1061\leq be_i \leq 10^6.

Translated by ChatGPT 5