CODPABBU - Pabbu-pneumonia

no tags 

There is a biology experiment going in the biotech. lab of the "very prestigious" DTU. The students there are trying to study the reproduction patterns of a newly discovered species of virus pabbu-pneumoniae ;)

These pabbu-pneumoniae virus reproduce in a rather strange and fascinating way.

After adding "a" number of viruses to the petri dish on the first day of the experiment they make the following observations:

On the second day, they found that the virus' number had been increased by "d".

On the third day, they found that the new number of viruses in the dish was "r" times the number recorded on the second day.

On the fourth day, they again found that the number had increased by "d" by that recorded on the third day... and so on...

The pabbu-pneumoniae virus never cease to reproduce.

For example, if d=1, r=4 and a=1..then the number of microbes recorded on each day would be: 1 2 8 9 36 37 148 149...

Given 'a', 'd', 'r' and 'n', you have to find the number of microbes in the petri dish at the end of the n'th day.

Since the numbers can be pretty large, you are required to print the answer modulo a number "m" ("m" will be supplied for each case)

Input

First line of input will have number 'T', the number of test cases. [T <= 2000]

Each of the test cases will have 2 lines -:

First line will have 3 space-separated numbers 'a','d'  and   'r' respectively.

2nd line will have 2 space-separated numbers 'n' and 'm' respectively.

Output

For each test case print the required answer in a separate line.

NOTE - The value of a, d, r, n and m will be more than 0 and less than 10^8.

Example

Input:
1
1 2 3
4 5

Output:
1

Explanation: The sequence is - 1, 3, 9, 11, 33, 35, 105... The 4th term is 11 and 11 % 5 = 1


hide comments
hodobox: 2017-04-26 03:44:10

Does asking for mod composite numbers really add to this :/

priyam195: 2017-01-08 09:41:13

WHAT IS THE maximum limits of the value of a,d,r,n,m ????
i have tried a better algorithm..but it still gives wrong answer....
Please check 18539491

rainy jain : 2016-06-09 03:29:02

Python->TLE
Cpp->AC

Last edit: 2016-06-09 03:50:18
Prateek chandan: 2014-08-25 23:50:48

@CSI : Tried checking the program against all test case but still it gives WA :(
Please check 12235014

Bhavik: 2014-06-16 21:20:57

@CSI: NOW I have a O(log(n)) solution and its giving wa...can u provide me a testcase where my code:11770748 fails

Last edit: 2014-06-16 21:21:26
CSI: 2014-01-09 17:13:22

@bhavik, u need a better algorithm.With an O(n) algorithm it will take hours to resolve some of the test cases

Bhavik: 2013-12-27 12:58:45

@CSI: i implemented properties of modulus effectively i guess but still tle...
kindly chk id:10748129 if i need to change my algo..thank you:)

CSI: 2013-12-26 01:21:19

@bhavik, a straight forward approach will time out.You need a better algorithm.

Bhavik: 2013-12-15 14:14:41

kindly chk my sol id:10651135
giving tle..or should i try different way to solve??


Added by:CSI
Date:2013-10-01
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64