TopCoder

User's AC Ratio

100.0% (2/2)

Submission's AC Ratio

50.0% (2/4)

Tags

Description

Each test case consist of $T$ cases.

Given $X, Y, M$. Print the minimum non-negative integer $K$ s.t. $X^ K \equiv Y (\bmod M)$, or $-1$ if there is no such $K$.

We note that $0^ 0 = 1$ in this problem.

Input Format

$T$
$X_ 0$ $Y_ 0$ $M_ 0$
$X_ 1$ $Y_ 1$ $M_ 1$
:
$X_ {T - 1}$ $Y_ {T - 1}$ $M_ {T - 1}$

Output Format

For each line, print answer.

Sample Input 1

7
2 1 5
4 7 10
8 6 10
5 2 11
5 9 11
0 0 1
0 2 4

Sample Output 1

0
-1
4
-1
4
0
-1

Hints

  • $1 \leq T \leq 100$
  • $0 \leq X, Y < M$
  • $1 \leq M \leq 10^ 9$

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 10000 2097152 2097152
1 10000 2097152 2097152
2 10000 2097152 2097152
3 10000 2097152 2097152
4 10000 2097152 2097152
5 10000 2097152 2097152
6 10000 2097152 2097152
7 10000 2097152 2097152
8 10000 2097152 2097152
9 10000 2097152 2097152
10 10000 2097152 2097152
11 10000 2097152 2097152
12 10000 2097152 2097152
13 10000 2097152 2097152
14 10000 2097152 2097152
15 10000 2097152 2097152
16 10000 2097152 2097152
17 10000 2097152 2097152
18 10000 2097152 2097152
19 10000 2097152 2097152