BETA

Difficulty Level:

Hard

Total Submissions: 888 Accuracy:

7.66%

Generalised Fibonacci numbers
         

Consider the generalized Fibonacci number G, which is dependent on a, b and c as follows :-
G(1) = 1
G(2) = 1
G(n) = aG(n-1) + bG(n-2) + c.
Your task is to calculate G(n)%m for given values of n and m.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains five space separated values a, b, c, n, m respectively.

Output:
For each test case in a new line print the required output denoting G(n).

Constraints:
1<=T<=100
1<=a, b, c, n, m<=10^9+7

Example:
Input:

2
3 3 3 3 5
2 2 2 4 100
Output:
4
16

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sanket yadav

Set Default Code

					   
It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.