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
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.
bruceewayne | 193 |
manvirag982 | 182 |
Core_Ka_Bachha | 180 |
shivam123ahuja | 175 |
Sanjana Singh 1 | 173 |
mr_kksparrow | 433 |
manvirag982 | 398 |
TheHandle | 291 |
rajupraaa1234 | 276 |
Exception_404 | 268 |
blackshadows | 5331 |
Ibrahim Nash | 5219 |
akhayrutdinov | 5111 |
mb1973 | 4929 |
Quandray | 4567 |