#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define repd(i,a,b) for(int i=a;i>=b;--i)
#define dbg(a,i,n) printf("%d %c",a," \n"[i==n])
using namespace std;
const int inf=0x3f3f3f3f;
const ll linf=(1ll<<62)-1;
const int N=3e5+7;
const int M=2e6+7;
const int mod=1e9+7;
template <typename T>inline void rd(T &x){
char c;int sign=1;x=0;
do{c=getchar();if(c=='-')sign=-1;}while(c<'0'||c>'9');
do{x=x*10+c-'0';c=getchar();}while(c>='0'&&c<='9');
x*=sign;
}
ll f[100][2][2], num[100];
ll dfs(int len,bool if61,bool if62,bool limit){
if(!len) return 1;
if(!limit && f[len][if61][if62]) return f[len][if61][if62];
ll ans = 0, up = limit? num[len]: 9;
for(int i=0; i<=up; i++){
if(if61 && if62 && i==6) continue;
ans += dfs(len-1, if62, i==6, limit && i==up);
}
return limit? ans: f[len][if61][if62] = ans;
}
ll check(ll n){
int k=0;
while(n){
num[++k] = n%10;
n/=10;
}
return dfs(k,false,false,true);
}
ll solve(ll n){
ll l = 0,r = (1ll<<63-1);
while(l<r){
ll mid = (l+r)/2;
if( mid+1-check(mid) < n) l = mid+1;
else r = mid;
}
return l;
}
int main(){
ll t,n;
rd(t);
while(t--){
rd(n);
printf("%lld\n",solve(n));
}
return 0;
}