Under construction

Still being built. Things may change

The Brick Stops Here

Time limit1sMemory limit128 MB

Summary
Given N brick types with copper content and price, answer C queries: pick exactly M distinct types whose copper sum lies in [M*Cmin, M*Cmax] at minimum total price.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Greedy, Array
Solved
No attempts yet

Problem

A factory manufactures brass bricks. Brass is an alloy of copper and zinc, and every brick weighs exactly 10001000 grams, of which the copper content may be anywhere from 11 to 999999 grams. The factory produces NN different types of brick; each type has its own copper content and its own price, and all types are listed in a catalog.

A client wants to buy exactly MM bricks, and they must all be of different types. The chosen bricks are melted together, and the resulting mixture must contain at least CminC_{min} and at most CmaxC_{max} grams of copper per kilogram. Because every brick weighs one kilogram, melting MM bricks yields MM kilograms whose copper content is the sum of the chosen copper values; the requirement is therefore

M⋅Cmin≤(sum of the chosen copper contents)≤M⋅Cmax.M \cdot C_{min} \le (\text{sum of the chosen copper contents}) \le M \cdot C_{max}.

Among all selections that satisfy this requirement, the client wants the one with the smallest total price. For each client, determine that minimum total price. The values MM, CminC_{min}, and CmaxC_{max} differ from client to client.

Input

The first line contains an integer NN (1≤N≤2001 \le N \le 200), the number of brick types. Each of the next NN lines contains two integers: the copper content (11 to 999999 grams) and the price (in cents) of that brick type. No brick costs more than 10001000 cents.

The next line contains an integer CC (1≤C≤1001 \le C \le 100), the number of clients. Each of the following CC lines contains three integers MM, CminC_{min}, and CmaxC_{max} (1≤M≤201 \le M \le 20, 1≤Cmin≤9991 \le C_{min} \le 999, 1≤Cmax≤9991 \le C_{max} \le 999), describing one client's request.

All input numbers are positive integers.

Output

For each client, print on its own line the minimum possible total price (in cents) for a valid selection of MM distinct brick types. If no selection meets the client's requirements, print impossible for that client.

Examples1

  1. Example 1

    Input
    11
    550 300
    550 200
    700 340
    300 140
    600 780
    930 785
    730 280
    678 420
    999 900
    485 390
    888 800
    3
    2 500 620
    9 550 590
    9 610 620
    
    Expected output
    420
    impossible
    3635