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

int main()
{
    ll t;cin>>t; while(t--){
        ll n;
        cin>>n;
        ll num =n;
        ll digit=0;
        ll ans=0;
        while(num>0){
            digit++;
            num/=10;
        }
        ans+=(digit-1)*9;
        ll aux=0;
        for(ll i=0;i<digit;i++){
            aux+=pow(10,i);  
        }
        ll i=0;
        for(;i<9;i++){
            if((i+1)*(aux) > n){
               // cout<<(i+1)<<" ";
                break;
            }
        }
        i--;
        ans+=(i+1);
        cout<<ans<<"\n";
    }
 
    return 0;
}