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

#define ll long long
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rev_loop(i,a,b) for(int i=a;i>b;i--)
#define pb push_back

int main()
{
    fast_io;

    int t;
    cin>>t;
    while(t--){
        double n;
        cin>>n;

        if(n<=9)
            cout<<n<<endl;
        else if(n<1e2)
            cout<<9+int(n/11)<<endl;
        else if(n<1e3)
            cout<<18+int(n/111)<<endl;
        else if(n<1e4)
            cout<<27+int(n/1111)<<endl;
        else if(n<1e5)
            cout<<36+int(n/11111)<<endl;
        else if(n<1e6)
            cout<<45+int(n/111111)<<endl;
        else if(n<1e7)
            cout<<54+int(n/1111111)<<endl;
        else if(n<1e8)
            cout<<63+int(n/11111111)<<endl;
        else
            cout<<72+int(n/111111111)<<endl;
        
    }
    return 0;
}