use std::iter::successors; fn main() { let mut lines = std::io::stdin().lines(); let n = { let line = lines.next().unwrap().unwrap(); line.trim().parse::().unwrap() }; for _ in 0..n { let line = lines.next().unwrap().unwrap(); let x = line.trim().parse::().unwrap(); if fast_is_prime(x) { println!("{x} 1"); } else { println!("{x} 0"); } } } fn pow_mod(x: u128, mut n: u128, p: u128) -> u128 { let mut a = x % p; let mut res = 1; while n != 0 { if n & 1 != 0 { res = (res * a) % p; } a = (a * a) % p; n >>= 1; } res } /// Check if the given integer is prime or not based on Miller-Rabin primality test. /// Time complexity is O(logn) pub fn fast_is_prime(n: u64) -> bool { // reference: // * https://miller-rabin.appspot.com // * https://drken1215.hatenablog.com/entry/2023/05/23/233000 const A: [u64; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]; match n { 0 | 1 => return false, 2 => return true, n if n & 1 == 0 => return false, _ => (), } let (s, d) = { let mut n = n - 1; let mut s = 0; while n & 1 == 0 { s += 1; n >>= 1; } (s, n) }; for a in A.into_iter().take_while(|&a| a < n) { let x = pow_mod(a as u128, d as u128, n as u128) as u64; if x != 1 { let xs = successors(Some(x), |&x| { Some(((x as u128 * x as u128) % n as u128) as u64) }); if xs.take(s).all(|x| x != n - 1) { return false; } } } true }