I won't tell you this is about number theory

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2685
64-bit integer IO format: %I64d      Java class name: Main

Description

To think of a beautiful problem description is so hard for me that let's just drop them off. :)
Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S.

Input

The first line contain a t,then t cases followed.
Each case contain four integers a,m,n,k(1<=a,m,n,k<=10000).

Output

One line with a integer S.

Sample Input

1
1 1 1 1

Sample Output

0

Source

Language: 
Theme: 
Share Code? 

Powered by NB231 | Current Style: .