#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
bool f[N];
int ans[N];
bool check(int x) {
while(x/=10) {
if(x%10==7) {
return true;
}
}
return false;
}
void init() {
int last=0;
for(int i=1; i<=N-1; i++) {
if (f[i]) {
continue;
}
if (check(i)) {
for (int j=1; i*j<=N-1; j++) {
f[i*j]=1;
}
continue;
}
ans[last]=i;
last=i;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
init();
int t;
cin>>t;
while(t--) {
int x;
cin>>x;
if(f[x]) {
cout<<-1;
} else {
cout<<ans[x];
}
cout<<"\n";
}
return 0;
}