#include<map>
#include<queue>
#include<time.h>
#include<limits.h>
#include<cmath>
#include<ostream>
#include<iterator>
#include<set>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep_1(i,m,n) for(int i=m;i<=n;i++)
#define mem(st) memset(st,0,sizeof st)
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef pair<double,double> pdd;
const int maxn = 1e9;
#define x first
#define y second
pair<int,int>pos[15];
int num[210];
int cnt=0;
bool check(int x)
{
bool f1=1;
int z=x%10;
while(x)//傻逼边界
{
//只能出现在右下
if (pos[z].x>=pos[x%10].x && pos[z].y>=pos[x%10].y)
{
z=x%10;
x/=10;
}
else
return 0;
}
return f1;
}
void init()
{
pos[1]= {1,1};
pos[2]= {1,2};
pos[3]= {1,3};
pos[4]= {2,1};
pos[5]= {2,2};
pos[6]= {2,3};
pos[7]= {3,1};
pos[8]= {3,2};
pos[9]= {3,3};
pos[0]= {4,2};
num[0]=0;
for(int i=0; i<=200; i++)
if(check(i))
num[++cnt]=i;
}
void solve()
{
int n;
cin>>n;
int t=1;
while(num[t]<=n && t<=cnt)
t ++;
if(num[t]-n>n-num[t-1])
t--;
cout<<num[t]<<endl;
}
signed main()
{
int t;
cin>>t;
init();
while(t--)
solve();
return 0;
}