#include <bits/stdc++.h>
using namespace std;
bool f1(int x)
{
ios::sync_with_stdio(false);
int temp=x;
if (temp%7==0)
{
return false;
}
if (temp%10%10==7)
{
return false;
}
int u;
while (temp!=0)
{
if (temp%10==7)
{
return false;
}
u=temp;
temp/=10;
}
if (u==7)
{
return false;
}
return true;
}
bool f2(int x)
{
for (int i=1;i*i<=x;i++)
{
if (x%i!=0)
{
continue;
}
if (f1(i)==false||f1(x/i)==false)
{
return false;
}
}
return true;
}
int p(int x)
{
while(!f2(x))
{
x++;
}
return x;
}
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;i++)
{
int x;
cin>>x;
if (!f2(x))
{
cout<<-1<<endl;
continue;
}
cout<<p(x+1)<<endl;
}
return 0;
}