BOMP - Financial rookie


You start your very first job as a junior stock-broker in a brokerage house. As you are a total rookie and only model for valuation option you know is binomal option pricing model thus your first task is to write a program which valuates current prices of european call and put options for given strikes in this model.

The binomal option pricing model provides numerical method for valuation of wide spectrum of options. The model assumes than in a given period of time T (maturity) price of share S0 (underlaying asset) changes n times (time passes (periods) between changes is equal). The price of share can move up by factor of u (uptick) with probability of p or down by factor of d (downtick) with probability of 1 - p. Furthermore we assue existance of risk-free rate r with continouse capitalization and no arbitrage which entail that probability p is equal to (exp(rT/n) - d)/(u - d). For example when n = 2 possible prices at time T are u2S0, udS0, d2S0, please see the image below.

 

We will focus only on valuation of current prices of two european option call and put. The pay-off function (this is what you receive at maturity when you buy the option) for call is w(x, K) = max(0, x - K) and for put w(x, K) = max(0, K - x), where K is the strike. The current price PI0 of the option is discounted expected value of pay-off function at time T, thus is given by the following formula

Input

The first line consist six numbers separated by single space n S0 u d r T, the number of periods, current share price, uptick, downtick, annual nominal risk-free rate, time to maturity. Next line consist one number m, the number of option to price. Next m lines consist two values separated by single space, the type of option (C for call option and P for put option) and strike K.

Restrictions:

0 < n < 200
0 < m <= 10000
0 < S0, K <= 1000
0 < d < exp(rT/n) < u < 2 and S0un < 1.79769e+308
0 < T <= 10

n, m are integers, the rest are decimal number with max three digits after decimal mark.

Output

For each test case write in the separate line the current value of option for given strike. The value should be rounded to two decimal places. The maximum absolute error of the value should be less than 0.01.

Example

Input:
1 100 1.5 0.5 0.0 1
1
C 100
Output:
25.0
Input:
4 300 1.2 0.9 0.1 2
2
P 300
C 300 Output: 12.66
67.04

 



Added by:GitPistachio
Date:2019-04-01
Time limit:0.100s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:own creativity