/**
链接:https://www.codechef.com/problems/REMMAX
题意:找1,2,3,...,n中,翻转之后的数值最大的那个数
eg: n==31 ans = 29
分析:主要是考虑这样几种情况:
1000000
1001999
1003769
1999999
3999999
2001111
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int solved(){
string ss;cin>>ss;
int sz=ss.size();
if(sz==1) return cout<<ss<<endl,0;
int ii;
for(ii=1;ii<sz;ii++) if(ss[ii]!='9') break;
if(ii==sz) {
return cout<<ss<<endl,0;
// 199999,299999
}
if(ss[0]=='1'){
int i;
for(i=1;i<sz;i++) if(ss[i]!='0') break;
if(i==sz) {
//1000000
for(int j=1;j<sz;j++) cout<<'9';
cout<<endl;
return 0;
}
else if(i==sz-1) {
// 1000003,1000009
string ans=ss;
return cout<<ans<<endl,0;
}
else {
//100376,
string ans;
for(int j=0;j<i;j++) ans+=ss[j];
bool ok=1;
for(int j=i+1;j<sz;j++) ok&=(ss[j]=='9');
if(ok) {
for(int j=i;j<sz;j++) ans+=ss[j];
return cout<<ans<<endl,0;//100019999
}
ans+=((ss[i]-'0')-1+'0');
for(int j=i+1;j<sz;j++) ans+='9';
return cout<<ans<<endl,0;
}
}
else {
string ans;
ans+=(((ss[0]-'0')-1)+'0');
for(int i=1;i<sz;i++) ans+='9';
return cout<<ans<<endl,0;
}
return cout<<endl,0;
}
int main (){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
int t;cin>>t;
while(t--) solved();
return 0;
}