9116: Semi-Puzzle: Brain Storm

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

Given two positive integers a,ma,ma,m. Find a non-negative integer uuu, where au≡u(modm)a^{u} \equiv u \pmod{m}auu(modm).

输入格式

There are multiple test cases.
The first line contains one integer TTT (1≤T≤10001\le T \le 10001T1000), 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^91a<m109).

输出格式

For each test case, output a line containing a single integer uuu (0≤u≤10180 \le u \le 10^{18}0u1018), representing the answer. It is guaranteed that the answer exists.

输入样例 复制

5
1 2
2 3
6 7
12 15
114514 1919810

输出样例 复制

3
10
36
96
104419494656

分类标签