#include<bits/stdc++.h>
using namespace std;
int n,x,last,nxt[10000009];
bool f[10000009];
bool pd(int x){
int res=x;
while(res){
int s=res%10;
if(s==7) return 1;
res/=10;
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=10000000;i++){
if(f[i]) continue;
if(pd(i)){
for(int j=i;j<=10000000;j+=i)
f[j]=1;
continue;
}
nxt[last]=i;
last=i;
}
for(int i=1;i<=n;i++){
cin>>x;
if(f[x]){
cout<<"-1"<<'\n';
continue;
}
cout<<nxt[x]<<'\n';
}
}
Wa了3个点。