#include <bits/stdc++.h>
using namespace std;
int n;
bool b[10001000];
bool cw(long long a){
while(a>0){
if(a%10==7) return 1;
a/=10;
}
return 0;
}
void cz(){
for(int i=2;i<=1500000;i++){
if(i%7==0 || cw(i)){
for(int j=1;j<=1500000,i*j<=10000010;j++){
b[i*j]=1;
}
}
}
}
int main(){
cin >> n;
cz();
for(int i=1;i<=n;i++){
long long x;
cin >> x;
if(b[x]==1) cout << -1 << endl;
else{
long long t=x+1;
while(b[t]) t++;
cout << t << endl;
}
}
return 0;
}