PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000390. Tặng tiền và tính thuế (Taxing Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tặng tiền và tính thuế (Taxing Problem)

Taxing Problem

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

George won the lottery and wishes to share money with his friends. A gift is added to the recipient’s annual income and taxed progressively. There are BB finite tax bands beginning at zero income. Band ii has width sis_i pounds and tax rate pi%p_i\%. Bands are consecutive and disjoint; income beyond all finite bands is taxed at rate P%P\%. For friend jj, George knows existing gross income eje_j and wants the friend to receive exactly mjm_j additional pounds after tax. Only tax arising from the gift is deducted from the gift. Find the gross gift for every friend. If multiple gifts give the required net amount, choose the smallest.

Input

The first line contains integer BB. The next BB lines, in increasing income-boundary order, each contain real numbers si,pis_i,p_i, the width and tax rate of band ii. The next line contains real number PP, the final tax rate. The next line contains integer FF, the number of friends. Each of the next FF lines contains real numbers ej,mje_j,m_j, existing gross income and desired additional net amount in pounds.

Output

Print FF lines in input order. Each line contains the smallest gross gift for the corresponding friend. Absolute or relative error at most 10−610^{-6} is accepted.

Subtasks

  • Subtask 1 (20 points): 1≤B≤11\le B\le 1; 0<si≤1060<s_i\le10^6; pi=0p_i=0 (1≤i≤B1\le i\le B); 0≤P≤99.9990\le P\le99.999; 1≤F≤201\le F\le20; 0≤ej≤1060\le e_j\le10^6; 0<mj≤1060<m_j\le10^6 (1≤j≤F1\le j\le F).
  • Subtask 2 (30 points): 1≤B≤51\le B\le 5; 0<si≤1060<s_i\le10^6; 0≤pi<1000\le p_i<100 (1≤i≤B1\le i\le B); 0≤P≤99.9990\le P\le99.999; 1≤F≤201\le F\le20; 0≤ej≤1060\le e_j\le10^6; 0<mj≤1060<m_j\le10^6 (1≤j≤F1\le j\le F).
  • Subtask 3 (50 points): 1≤B≤201\le B\le 20; 0<si≤1060<s_i\le10^6; 0≤pi≤1000\le p_i\le100 (1≤i≤B1\le i\le B); 0≤P≤99.9990\le P\le99.999; 1≤F≤201\le F\le20; 0≤ej≤1060\le e_j\le10^6; 0<mj≤1060<m_j\le10^6 (1≤j≤F1\le j\le F).

Examples

Input

1
1000 0
20
3
0 500
999.5 500
1000 500

Output

500
624.875
625

Explanation

The first friend has enough untaxed income space, so receives 500. The second receives 0.5 untaxed and needs 499.5 net at 20% tax, costing 624.375; total 624.875. The third has exhausted the first band, so needs 500/0.8=625.

Input

3
4750.5 0
8000 20
10000 40
60
3
0 10000
10000 5000
15000 5000

Output

11312.375
7416.5
8624.75

Explanation

The first gets 4750.5 untaxed and needs 5249.5 net at 20%, costing 6561.875; total 11312.375. The second gets 2200.4 from 2750.5 at 20%, then 2799.6 net at 40%, costing 4666; total 7416.5. The third gets 4650.3 from 7750.5 at 40%, then 349.7 net at 60%, costing 874.25; total 8624.75.

Input

2
100 0
100 100
50
2
0 100
0 101

Output

100
202

Explanation

The first friend needs only 100 in the untaxed band; the next 100 would be fully taxed, so 100 is the smallest valid gift. The second needs 101; the first 200 yields 100 net, and 2 at the 50% rate adds 1, totaling 202.