#include<bits/stdc++.h>
using namespace std;
long long t,a;
string b;
int s[100000009];
inline long long p(long long x){
if(s[x]==1)return 1;
else if(s[x]==-1)return 0;
if(x%7==0){s[x]=1;return 1;}
long long y=x;
while(y>=10){
if(y%10==7){s[x]=1;return 1;}
else y/=10;
}
if(y%10==7){s[x]=1;return 1;};
for(int i=2;i<=sqrt(x);i++){
if(x%i==0){
if(p(i)){s[x]=1;return 1;}
if(p(x/i)){s[x]=1;return 1;};
}
}
s[x]=-1;
return 0;
}
inline long long read(){
b=getchar();
if(b=="7")return 1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
cin>>a;
if(p(a)){cout<<-1<<endl;continue;}
a++;
while(p(a))a++;
cout<<a<<endl;
}
return 0;
}