#P184. [Extra Contest #4] Factorial Grandmaster

[Extra Contest #4] Factorial Grandmaster

Person in Charge

Attention

is the easy version of this problem.

This problem requires file I/O (factorial.in / factorial.out).

Problem Background

The lottery results are out. Sleeping Dolphin was surprised to find that he didn't win because... he accidentally bought a different lottery ticket.

Sleeping Dolphin checked his phone—it was already 1:00 AM on April 13th.

Time to focus on setting problems for Sleeping Cup. Let's get to work.

Problem Description

Find the maximum positive integer solution xx to the following equation:

x!1 (mod n)x! \equiv 1\ (\bmod\ n)

where x!=1×2××xx! = 1 \times 2 \times \ldots \times x.

If no such positive integer solution exists, output 00.

Input Format

There are multiple test cases.

The first line contains a positive integer T (1T106)T\ (1 \le T \le 10^6) indicating the number of test cases.

Each of the next TT lines contains a positive integer n (1n109)n\ (1 \le n \le 10^9).

Output Format

Output TT lines, each containing a non-negative integer representing the answer.

Samples

5
1
2
3
4
5
0
1
1
1
3
5
118
119
120
121
122
1
5
1
9
1

Sample 1 Explanation

#\color{blue}\# 1! (=1)\color{blue}1!\ (=1) 2! (=2)\color{blue}2!\ (=2) 3! (=6)\color{blue}3!\ (=6) 4! (=24)\color{blue}4!\ (=24) 5! (=120)\color{blue}5!\ (=120) \color{blue}\ldots
mod 1\color{blue}\bmod\ 1 =0\color{red}=0 =0\color{red}=0 =0\color{red}=0 =0\color{red}=0 =0\color{red}=0 =0\color{red}=0
mod 2\color{blue}\bmod\ 2 =1\color{red}=1 =0=0 =0=0 =0=0 =0=0 =0=0
mod 3\color{blue}\bmod\ 3 =1\color{red}=1 =2=2 =0=0 =0=0 =0=0 =0=0
mod 4\color{blue}\bmod\ 4 =1\color{red}=1 =2=2 =2=2 =0=0 =0=0 =0=0
mod 5\color{blue}\bmod\ 5 =1\color{red}=1 =2=2 =1\color{red}=1 =4=4 =0=0 =0=0

From the red-marked parts in the table above, the answers for the sample are 0,1,1,1,30,1,1,1,3.

Note that when n=1n=1, the equation has infinitely many positive integer solutions (1mod1=01 \bmod 1 = 0), so the answer is 00.