#include<bits/stdc++.h>
using namespace std;
int t,x,s;
int pd(int x){
if(x%7==0) return 1;
while(x%2==0){
int y=x;
while(y>1){
s=y%10;
if(s==7) return 1;
y/=10;
}
x/=2;
}
int y=x;
while(y>1){
s=y%10;
if(s==7) return 1;
y/=10;
}
return 0;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&x);
if(pd(x)) cout<<-1<<endl;
else{
for(;;){
x++;
if(pd(x)==0){
cout<<x<<endl;
break;
}
}
}
}
return 0;
}