#include<iostream>
using namespace std;
const int N=1e7+10;
inline int read(){
int x=0,f=1;char ch=getchar_unlocked();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar_unlocked();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar_unlocked();
}
return f*x;
}
bool so(int x){
while(x){
if(x%10==7) return true;
x/=10;
}
return false;
}
long long T,f[N],a;
int main(){
for(int i=7;i<=N-10;i++){
if(so(i)){
for(int j=1;j*i<=N-10;j++){
if(f[i*j]!=0) continue;
f[i*j]=1;
}
}
}
for(int i=N-10;i>=1;i--){
if(f[i]==1) f[i]=f[i+1];
else f[i]=i;
}
T=read();
for(int i=1;i<=T;i++){
a=read();
if(a!=f[a]) printf("-1\n");
else printf("%lld\n",f[a+1]);
}
return 0;
}