Under construction

Still being built. Things may change

Gift Before Tax

Time limit2sMemory limit512 MB

Summary
Given progressive tax bands and each friend's prior income, find the smallest pre-tax gift that leaves the friend with exactly the required after-tax amount.
Level

Medium6 of 10

Topics
Binary search, Math, Implementation
Solved
No attempts yet

Problem

George has won the lottery and wants to share the money with his friends. A gift is taxed once it gets large enough, and the tax depends on how much the friend has already earned that year.

Tax is worked out with tax bands. The bands start at zero and follow one another. Each band taxes the part of the income that falls inside it at its own rate. The last band applies to all income above the sum of the sizes of the other bands.

If a friend has already earned ee this year and George hands over a pre-tax amount gg, that friend's income for the year rises from ee to e+ge+g. The tax due is the total tax on income e+ge+g minus the total tax on income ee, and the friend keeps gg minus that tax.

You are given the number of tax bands, how much each friend has earned, and how much each friend has to keep after tax. Work out how much George gives each friend before tax.

Input

  • One line with an integer BB (1≤B≤201 \le B \le 20), the number of tax bands.
  • BB lines, each with two real numbers sis_i (0<si≤1060 < s_i \le 10^6) and pip_i (0≤pi≤1000 \le p_i \le 100): the size in pounds of the ii-th tax band and its tax rate in percent.
  • One line with a real number PP (0≤P≤99.9990 \le P \le 99.999), the tax rate in percent on all income above the bands.
  • One line with an integer FF (0<F≤200 < F \le 20), the number of friends.
  • FF lines, each with two real numbers eje_j and mjm_j (0≤ej≤1060 \le e_j \le 10^6, 0<mj≤1060 < m_j \le 10^6): what the jj-th friend has earned, and what that friend has to keep after tax.

The tax bands are given from the lowest band to the highest.

Output

Print FF lines, one per friend, in the order the friends are given.

Each line holds the pre-tax amount George gives that friend, rounded to exactly six decimal places. When several amounts leave the friend with exactly mjm_j, print the smallest one.

Examples2

  1. Example 1

    Input
    1
    1000 0
    20
    3
    0.0 500
    999.5 500
    1000.0 500
    
    Expected output
    500.000000
    624.875000
    625.000000
    
  2. Example 2

    Input
    3
    4750.50 0
    8000 20
    10000 40
    60
    3
    0 10000
    10000 5000
    15000 5000
    
    Expected output
    11312.375000
    7416.500000
    8624.750000