
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
   
    while(t--){
        int n;
        cin>>n;
      
        long long int count=0,i;
        for(auto j=1;j<=9;j++){
            i=j;
            while(i<=n){
                count+=1;
                i=(i*10)+j;
                
                }
        }
         cout<<count<<endl;
            
        
    }

    return 0;
}
