def pow_mod(a, k, n) ret = 1 while k > 0 if k.odd? ret = (ret * a) % n end a = (a ** 2) % n k >>= 1 end ret end def miller_rabin(n, bases) d = n - 1 s = 0 while d.even? d = d >> 1 s = s + 1 end for a in bases do if n <= a return true end a = pow_mod(a, d, n) if a == 1 then else r = 1 while a != n - 1 do if r == s then return false end a = a * a % n r = r + 1 end end end return true end def is_prime(n) return false if n < 2 return true if n < 4 return false if n.even? return miller_rabin(n, Array[2, 7, 61]) if n < 4759123141 return miller_rabin(n, Array[2, 325, 9375, 28178, 450775, 9780504, 1795265022]) end N = gets.to_i N.times do x = gets.to_i if is_prime(x) puts [x, 1].join(' ') else puts [x, 0].join(' ') end end