#include <bits/stdc++.h>
#define ll long long
using namespace std;
int t,cnt;
const int N=1e6+10,M=1e7+10;
int IS[N],v[N];
bool NW(int x){
while(x>0){
int p=x%10;
x/=10;
if(p==7) return 0;
}
return 1;
}
void SH(){
for(int i=1;i<=M;i++){
if(!v[i]){
if(NW(i)){
// v[i]=++cnt;
// IS[cnt]=i;
}
//else{
//for(int j=i;j<=M;j+=i) v[j]=-1;
//}
//
}
}
}
signed main(){
SH();
scanf("%d",&t);
while(t--){
int x;
scanf("%d",&x);
if(v[x]==-1){
puts("-1");
continue;
}
printf("%d\n",IS[v[x]+1]);
}
return 0;
}