#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
vector<int>v;
set<string>s;
void dfs(int id,int now,bool qd){
if(!id){
if(now>=1&&now+2<=inf){
string gg=to_string(now)+to_string(now+1);
sort(gg.begin(),gg.end());
if(!s.count(gg)) s.insert(gg),v.push_back(now);
}
return;
}
if(qd){
dfs(id-1,now*10+9,qd);
return;
}
int w=0;
if(now>=10&&w!=1) w=now%10;
for(int i=0;i<10;i++) dfs(id-1,now*10+i,qd|(w>i));
}
int main(){
ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(0);
sort(v.begin(),v.end());
dfs(9,0,0);
int q;cin>>q;
while(q--){
int r;cin>>r;
cout<<upper_bound(v.begin(),v.end(),r)-v.begin()<<"\n";
}
return 0;
}