#include<bits/stdc++.h>
using namespace std;
#define N 300005
int T;
inline int read()
{
int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9')
{
s=s*10+ch-'0';ch=getchar();
}
return s*w;
}
inline void print(int x)
{
if(x<0)
{
putchar('-');print(-x);return ;
}
if(x<10)
{
putchar('0'+x);return ;
}
print(x/10);print(x%10);
return;
}
int ip[2*N],pr[2*N],cnt;
inline bool ck(int x)
{
if(ip[x])return 1;
while(x)
{
if(x%10==7)return 1;
x/=10;
}
return 0;
}
int pt[2*N],tot;
void init()
{
for(int i=1;i<=N-5;i++)
{
for(int j=1;j*j<=i;j++)
{
if(i%j!=0)continue;
if(ck(i/j)||ck(j))
{
ip[i]=1;pr[++cnt]=i;
}
}
if(!ip[i]&&ck(i))
{
pr[++cnt]=i;ip[i]=1;
}
if(!ip[i])
{
pt[++tot]=i;
}
}
}
void solve()
{
cin>>T;
while(T--)
{
int x;
cin>>x;
if(ip[x])
print(-1);
else
{
int l=1,r=tot;
while(l!=r)
{
int mid=(l+r)/2;
if(pt[mid]<=x)
{
l=mid+1;
}
else
r=mid;
}
print(pt[l]);
}
putchar('\n');
}
}
signed main()
{
init();
solve();
return 0;
}