#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e9+1;
const ll mod=1e9+7;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);  
    cout.tie(NULL); 
    int t;
    cin>>t;
    for(int tt=1;tt<=t;tt++){
        ll n;
        cin>>n;
        ll cnt=0;
        ll proc=0;
        for(int i=1;i<=9;i++){
            proc=i;
            while(proc<=n){
                proc=proc*10+i;
                cnt++;
            }
        }
        cout<<cnt<<endl;
        // cout<<"Case #"<<tt<<": "<<nums[index]<<endl;
    }
    // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    return 0;
}