#include <bits/stdc++.h>
using namespace std;

/************************userid:-code_dragon007 ***********************************************************/

#define ll long long 
#define MOD 1000000007
#define pb push_back 
#define GCD __gcd
#define endl "\n"

void runfast(){
 ll int n,temp,res=0,rem=0,value=1,c=0,ord_no=0,add,digits;
 bool flag=false;
 cin>>n;
 if(n<10){
     cout<<n<<endl;
     return ;
 }
 res+=9;
 value=10;
   while(value*10<=n){
       res+=9;
       value*=10;
   }
   //value/=10;
   digits=floor(log10(n) + 1);
   add=n/pow(10,digits-1);
   for(int i=0;i<digits;i++)
   ord_no*=10,ord_no+=add;
   if(ord_no<=n){
       res+=add;
   }
   else
   res+=(add-1);
    cout<<res<<endl;

}
/*********READY TO FACE WRATH OF TESTCASES  GOOD LUCK!!!*********************************************/
int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    int t;
    cin>>t;
    while(t--){
    runfast();
}
}
