#P7706. [2019年杭电多校]discrete logarithm problem

[2019年杭电多校]discrete logarithm problem

discrete logarithm problem

Problem Description

You are given three integers p,a,bp, a, b, where pp is a prime number and p1p-1 only has prime factors 22 and/or 33. Please find the minimum positive integer xx such that axb(modp)a^x \equiv b \pmod{p}.

Input

The first line contains an integer TT indicating there are TT tests. Each test consists of a single line containing three integers: p,a,bp, a, b.

  • T200T \le 200
  • 65537p101865537 \le p \le 10^{18}
  • the prime factors of p1p-1 can only be 22 or 33
  • 2a,bp12 \le a, b \le p-1

Output

For each test, output a line containing an integer xx, representing the minimum positive value such that axb(modp)a^x \equiv b \pmod{p}. If there didn't exist any such number xx, please output 1-1.

Sample Input

6

65537 2 3

65537 2 4

65537 3 4

65537 4 4

65537 5 4

666334875701477377 2 3

Sample Output

-1

2

45056

1

36864

1957714645490451

Source

2019 Multi-University Training Contest 5