#arc172e. E - Last 9 Digits

E - Last 9 Digits

问题陈述

判断是否存在正整数 nn ,使得 nnn^n 除以 10910^9 的余数为 XX 。如果存在,找出最小的 nn

您需要解决 QQ 个测试用例。

以上为机器翻译结果,仅供参考。

Problem Statement

Determine whether there is a positive integer nn such that the remainder of nnn^n divided by 10910^9 is XX. If it exists, find the smallest such nn.

You will be given QQ test cases to solve.

Constraints

  • 1Q100001 \leq Q \leq 10000
  • 1X10911 \leq X \leq 10^9 - 1
  • XX is neither a multiple of 22 nor a multiple of 55.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where XiX_i is the value of XX in the ii-th test case (1iQ)(1 \leq i \leq Q):

QQ

X1X_1

X2X_2

\vdots

XQX_Q

Output

Print QQ lines. The ii-th line should contain the answer for the ii-th test case. If no nn satisfies the condition, the answer should be 1-1.

Sample Input 1

2
27
311670611

Sample Output 1

3
11

This sample input consists of two test cases.

  • The first case: The remainder of 33=273^3 = 27 divided by 10910^9 is 2727, so n=3n = 3 satisfies the condition.
  • The second case: The remainder of 1111=28531167061111^{11} = 285311670611 divided by 10910^9 is 311670611311670611, so n=11n = 11 satisfies the condition.
}