#include<bits/stdc++.h>
using namespace std;
long long f[10000002],dui[10000002],ans[10000002];
long long T,cnt=0,y;
inline int read()
{
register long long x=0,f=1; register char ch; ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48); ch=getchar();}
return x*f;
}
inline bool check(int x)
{
while(x>0)
{
if(x%10==7) return true;
x/=10;
}
}
inline void cyh()
{
for(int i=1;i<=10000001;i++)
{
if(f[i]==1) continue;
else if(check(i))
{
for(int j=i;j<=10000001;j+=i) f[j]=1;
continue;
}
dui[i]=++cnt;
ans[cnt]=i;
}
}
int main()
{
T=read();
cyh();
while(T--)
{
y=read();
if(f[y]==1) printf("-1\n");
else cout<<ans[dui[y]+1]<<endl;
}
return 0;
}