#nt803. 指数方程1

指数方程1

Exponential Equation 1

Problem Description

Answer TT queries. Each query gives three numbers a,b,ma, b, m; output the smallest non-negative integer xx satisfying ax≡b(modm)a^x \equiv b \pmod m.

Input Format

The first line contains an integer TT. The next TT lines each contain three integers a,b,ma, b, m.

Output Format

One integer: the smallest non-negative solution; if no solution exists, output -1.

Sample Input

4
3 7 10
688720331 114514 998244353
5 114514 998244353
688720331 407057022 998244353

Sample Output

3
-1
271880075
123

Constraints

For all data, T≤100T\leq 100, 1≤a,b<m≤1091\leq a, b < m \leq 10^9 are guaranteed, and aa is coprime to mm.