Given two positive integers a,ma,ma,m. Find a non-negative integer uuu, where au≡u(modm)a^{u} \equiv u \pmod{m}au≡u(modm).
输入格式
There are multiple test cases.
The first line contains one integer TTT (1≤T≤10001\le T \le 10001≤T≤1000), representing the number of test cases.
Each of the following TTT lines contains two integers a,ma,ma,m (1≤a<m≤1091\le a < m \le 10^91≤a<m≤109).
输出格式
For each test case, output a line containing a single integer uuu (0≤u≤10180 \le u \le 10^{18}0≤u≤1018), representing the answer. It is guaranteed that the answer exists.