IDC1948 - Identity crisis

For every given number n we define x(n) as distance from n to the first number after n in form of 99...99. For example x(100)=899, x(45)=54, etc. Given several n numbers you have to find the Zp, where x(n) ≡ n (mod p).

Input

First line of input contains one number T (T < 20) - the number of test cases. In each of the next T lines contains one number each to represent n (0 < n < 30000000).

Output

In each line you have to write one number - the least p > 1 that x(n) ≡ n (mod p). If there is no such p the line should contain -1.

Example

Input:
2
234
5

Output:
3
-1

Explanation

x(234)=765. 765 mod 3=0, 234 mod 3=0 => 765 ≡ 234 (mod 3)


Added by:Konrad Krystecki
Date:2014-02-04
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64

hide comments
2014-07-08 23:01:17 Anshul Singhal
@Konrad Krystecki what is the x(n) for n=99.....Is it 900 or 0 ......Please reply fast

Last edit: 2014-07-08 23:09:21
2014-06-02 20:40:21 P_Quantum
nice!!
2014-05-24 14:15:33 [Lakshman]
@Tarun Garg So give a try to http://www.spoj.com/problems/LKID
2014-05-24 12:40:44 Tarun Garg
seive,prime,elementary maths..:D
2014-03-28 20:13:06 [Lakshman]
After solving this you may try http://www.spoj.com/problems/LKID with hard constraints.

Last edit: 2014-03-29 03:39:57
2014-03-24 05:20:59 nikhil_nihal
unlucky 13..
getting wrong ans. at 13th case.
help plz.. @Konrad Krystecki
2014-02-10 07:25:31 Jumpy
don't read comments they'll make you confuse only check the output of @Mostafa
2014-02-08 10:18:56 RIVU DAS
Good ques!!
2014-02-08 04:25:43 [Lakshman]
@ Mehmet Inal I think there is no math only the proper algorithm which Mitch is talking about, I was also confused with that comment
2014-02-08 02:49:04 mehmetin
I had already AC'ed in 0.00s with something I use a lot, but I wonder if it can be done in one formula and O(1), doing maths, as some people pointed out.

Last edit: 2014-02-08 02:51:05
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.