SEQAGAIN - Easy Sequence!

no tags 

Your task is to find the nth term of the following sequence :

F(n) = [F(n-1)*F(n-2)]K for n>1

F(0), F(1), n and K will be provided as input. Modulus for all calculations is 1000000007. You should print the answer modulo 1000000007 i.e. F(n)%1000000007

Input

Input starts with a line containing an integer T ≤ 5000 which is the number of test cases in the file. Your program will be run on several input files.

Each test case consists of four space separated integers : F(0), F(1), n and K.

Output

T lines containing one integer each, corresponding to the answers for the T test cases.

Constraints

0 ≤ n ≤ 1018

0 ≤ K ≤ 109

0 ≤ F(0), F(1) ≤ 106

Example

Input:
1
1 1 2 1

Output: 1

hide comments
Francky: 2014-06-05 03:04:03

Nice problem, but time limit seems too strict for python. 5s could be enough and would not allow 'bad' fast langage solutions, IMHO.
RE: Time limit is now 5s. Thank you for pointing that out.
RE RE: 5s is not enought for python, imho. I did my best.

Last edit: 2012-06-27 09:25:34
zingoba: 2014-06-05 03:04:03

Rejudged. Some of the previously AC'ed solutions now give WA.

Last edit: 2012-06-26 22:12:35

Added by:zingoba
Date:2012-06-26
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64