EBOXES - Empty Boxes

N large empty boxes (assume they are of type:1) are initially placed on a table. An unknown number of boxes (type:1) are selected and in each of them K smaller boxes (type:2) are placed. Again an unknown number of type:2 boxes are selected and K boxes of type:3 are placed inside. This process is repeated T times. Now a box is assumed to be empty when it has no smaller boxes inside it. Finally after all the processes are complete let there be F empty boxes in total.

Limits

1< N, K, T, F <1000000

Input

First line of the input file contains the number of test cases. Then each line contains 4 integers N, K, T, F as described above.

Output

Each line should contain the total number of boxes on the table.

Example

Input:
1
11 8 2 102

Output:
115

Added by:abhijith reddy d
Date:2009-05-07
Time limit:2.369s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET

hide comments
2015-01-31 07:14:42 Arnabjyoti Kalita
Somebody, please provide more test cases...
I seem to be getting the correct output for the above test case...but spoj giving me WA!!!
2014-12-30 09:17:58 gh0sth0und
@hummblefool_2 @Rydel
T is used in derivation
easy math :)
2014-12-25 20:00:05 :.Mohib.:
Nice problem....AC in 1 go..... :)
2014-12-22 19:05:45 kshitij tripathi
Easy mathematics..
Accepted in 1st go!! :)
2014-12-10 19:34:25 DEEPAK KUMAR SINGH
@UJtriumphs -thanx for the link.Simple math.Nic Problem..
2014-12-04 20:46:55 Vamsi Krishna Avula
basic math, nice problem.
2014-10-06 15:50:17 shreya sahu
logical one. bit of maths + fun :)
2014-10-05 11:28:58 UJtriumphsâ„¢
Helped a lot->https://www.physicsforums.com/threads/help-with-empty-boxes-algorithmic-problem.204043/
2014-10-05 06:20:59 Riddhi
@Krzysztof
thanks
2014-08-18 16:18:16 Krzysztof Suwiñski
Putting N large empty boxes isn't part of the repeatable process! It wasn't clear for me. Hope it helps someone.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.