#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
template<class T> struct cLtraits_identity{
using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
typename conditional<
is_integral<T>::value,
make_signed<T>,
cLtraits_identity<T>
>::type;
template <class S, class T> struct cLtraits_common_type{
using tS = typename cLtraits_try_make_signed<S>::type;
using tT = typename cLtraits_try_make_signed<T>::type;
using type = typename common_type<tS,tT>::type;
}
;
template<class S, class T> inline auto max_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
return (typename cLtraits_common_type<S,T>::type) a >= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
#define ISPRIME_PRE_CALC_SIZE 1000000
char isPrime_prime_table[ISPRIME_PRE_CALC_SIZE];
template<class T> inline int isPrime(T n);
void isPrime32_init(void);
int isPrime32_sub(int b, unsigned n);
int isPrime32(unsigned n);
int isPrime64_sub(long long b, unsigned long long n);
int isPrime64(unsigned long long n);
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline int rd_int(void){
int x;
rd(x);
return x;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
template<class T> inline T pow2_L(T a){
return a*a;
}
inline long long Isqrt_f_L(const long long n){
long long r = sqrt(n);
r =max_L(r-2, 0);
while((pow2_L((r+1)))<= n ){
r++;
}
return r;
}
int main(){
int Lj4PdHRW;
{
isPrime32_init();
}
int KL2GvlyY = rd_int();
for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){
int X;
rd(X);
if(X==0){
wt_L(3);
wt_L('\n');
continue;
}
if(isPrime(X)){
wt_L(1);
wt_L('\n');
continue;
}
if(isPrime(X+X+1) || isPrime(X+X-1)){
wt_L(2);
wt_L('\n');
continue;
}
if(X >= 0){
X =X;
}
else{
X =-X+1;
}
for(;;){
if(isPrime(X)){
wt_L(2*X);
wt_L('\n');
break;
}
if(isPrime(X+X+1)){
wt_L(2*X+1);
wt_L('\n');
break;
}
X++;
}
}
return 0;
}
template<class T> inline int isPrime(T n){
T i;
if(n<=1){
return 0;
}
if(n <= (1ULL<<32) - 1){
return isPrime32(n);
}
if(n <= (1ULL<<63) - 1 + (1ULL<<63)){
return isPrime64(n);
}
if(n<=3){
return 1;
}
if(n%2==0){
return 0;
}
for(i=3;i*i<=n;i+=2){
if(n%i==0){
return 0;
}
}
return 1;
}
int isPrime32_sub(int b, unsigned n){
unsigned i;
unsigned t = 0;
unsigned u = n-1;
unsigned long long nw;
unsigned long long nx;
while(!(u&1)){
t++;
u >>= 1;
}
nw = 1;
nx = b % n;
while(u){
if(u&1){
nw = (nw * nx) % n;
}
nx = (nx * nx) % n;
u >>= 1;
}
for(i=(0);i<(t);i++){
nx = (nw * nw) % n;
if(nx == 1 && nw != 1 && nw != n-1){
return 0;
}
nw = nx;
}
if(nw == 1){
return 1;
}
return 0;
}
int isPrime32(unsigned n){
if(n < 100000){
return isPrime_prime_table[n];
}
if(n % 2 == 0){
return 0;
}
if(!isPrime32_sub(2,n)){
return 0;
}
if(n<=1000000){
if(!isPrime32_sub(3,n)){
return 0;
}
}
else{
if(!isPrime32_sub(7,n)){
return 0;
}
if(!isPrime32_sub(61,n)){
return 0;
}
}
return 1;
}
int isPrime64_sub(long long b, unsigned long long n){
unsigned long long i;
unsigned long long t = 0;
unsigned long long u = n-1;
__uint128_t nw;
__uint128_t nx;
while(!(u&1)){
t++;
u >>= 1;
}
nw = 1;
nx = b % n;
while(u){
if(u&1){
nw = (nw * nx) % n;
}
nx = (nx * nx) % n;
u >>= 1;
}
for(i=(0);i<(t);i++){
nx = (nw * nw) % n;
if(nx == 1 && nw != 1 && nw != n-1){
return 0;
}
nw = nx;
}
if(nw == 1){
return 1;
}
return 0;
}
int isPrime64(unsigned long long n){
if(n < 100000){
return isPrime_prime_table[n];
}
if(n < (1ULL<<32)){
return isPrime32(n);
}
if(n % 2 == 0){
return 0;
}
if(!isPrime64_sub(2,n)){
return 0;
}
if(n <= 21652684502221ULL){
if(!isPrime64_sub(1215,n)){
return 0;
}
if(!isPrime64_sub(34862,n)){
return 0;
}
if(!isPrime64_sub(574237825,n)){
return 0;
}
}
else{
if(!isPrime64_sub(325,n)){
return 0;
}
if(!isPrime64_sub(9375,n)){
return 0;
}
if(!isPrime64_sub(28178,n)){
return 0;
}
if(!isPrime64_sub(450775,n)){
return 0;
}
if(!isPrime64_sub(9780504,n)){
return 0;
}
if(!isPrime64_sub(1795265022,n)){
return 0;
}
}
return 1;
}
void isPrime32_init(void){
int i;
int j;
int k;
k =Isqrt_f_L(ISPRIME_PRE_CALC_SIZE);
for(i=(2);i<(ISPRIME_PRE_CALC_SIZE);i++){
isPrime_prime_table[i] = 1;
}
for(i=(2);i<(k+1);i++){
if(isPrime_prime_table[i]){
for(j=(i*i);j<(ISPRIME_PRE_CALC_SIZE);j+=(i)){
isPrime_prime_table[j] = 0;
}
}
}
}
// cLay version 20210926-1
// --- original code ---
// REP(rd_int()){
// int @X;
// if(X==0) wt(3), continue;
// if(isPrime(X)) wt(1), continue;
// if(isPrime(X+X+1) || isPrime(X+X-1)) wt(2), continue;
// X = if[X >= 0, X, -X+1];
// for(;;){
// if(isPrime(X)) wt(2*X), break;
// if(isPrime(X+X+1)) wt(2*X+1), break;
// X++;
// }
// }