Problem G. Ipad,IPhone

时间限制 1000 ms   内存限制 32 MB

In ACM_DIY, there is one master called “Lost”. As we know he is a “-2Dai”, which means he has a lot of money.
  

Well, Lost use Ipad and IPhone to reward the ones who solve the following problem.
  

In this problem, we define F( n ) as :
  

Then Lost denote a function G(a,b,n,p) as

Here a, b, n, p are all positive integer!
If you could tell Lost the value of G(a,b,n,p) , then you will get one Ipad and one IPhone!
 

输入数据

The first line is one integer T indicates the number of the test cases. (T <= 100)
Then for every case, only one line containing 4 positive integers a, b, n and p.
(1 ≤a, b, n, p≤2*10 9 , p is an odd prime number and a,b < p.)
 

输出数据

Output one line,the value of the G(a,b,n,p) .
 

样例输入

复制
4
2 3 1 10007
2 3 2 10007
2 3 3 10007
2 3 4 10007

样例输出

复制
40
392
3880
9941

提交

请先 登录

© 2025 FAQs Contact About