cho.sh
Notes
Loading...

Composite Prime

Time limit

1s

Memory limit

1024 MB

Problem

131313131313 factors as 13×10113 \times 10113×101, so it is composite. However, every proper contiguous subnumber of 131313131313 made from at least two consecutive digits, namely 131313, 313131, 131313, 131131131, and 313313313, is prime.

For a positive integer NNN with at least three digits, define a proper contiguous subnumber as follows. Take two or more consecutive digits from the decimal representation of NNN, keep their order, and interpret the result as a nonnegative integer. The number NNN itself is excluded.

A positive integer NNN is called a composite prime if all three conditions below hold.

  • NNN has at least three digits.
  • NNN is composite.
  • Every proper contiguous subnumber of NNN is prime.

A proper contiguous subnumber may start with 000, and the value 000 itself is possible. For example, the proper contiguous subnumbers of 200232002320023 include 202020, 00(=0)00(=0)00(=0), 02(=2)02(=2)02(=2), 232323, 200200200, 002(=2)002(=2)002(=2), 023(=23)023(=23)023(=23), 200220022002, and 0023(=23)0023(=23)0023(=23). Since 202020 is not prime, 200232002320023 is not a composite prime.

Given a positive integer NNN, find the largest composite prime not greater than NNN.

Input

The first line contains the number of test cases TTT. (1≤T≤105)(1 \le T \le 10^5)(1≤T≤105)

Each of the next TTT lines contains one integer NNN. (1≤N≤107)(1 \le N \le 10^7)(1≤N≤107)

Output

For each test case, print the largest composite prime not greater than NNN on its own line. If no such number exists, print −1-1−1 instead.

Print the answers in the same order as the input test cases.