Ivan the Terrible

Given three integer numbers nn, aa and bb, does there exist a natural tt such that at≡bmod⁡na^t \equiv b \bmod n?

Input

Input consists of the number of cases cc, followed by cc triples with nn, aa and bb. You can assume 2≤n≤1092 \le n \le 10^9, 0≤a<n0 \le a < n, and 0≤b<n0 \le b < n. Additionally, assume c≤200c \le 200 for the “hard private test cases”.

Output

For each case, print “YES” or “NO” depending on whether at≡bmod⁡na^t \equiv b \bmod n has at least one solution t≥0t \ge 0 or not.

Problem information

Author: Ivan Geffner

Generation: 2026-01-25T10:31:11.350Z

© Jutge.org, 2006–2026.
https://jutge.org