#include <bits/stdc++.h>
using namespace std;typedef long long ll;
void swa(int &x, int &y){int temp = x;x = y;y = temp;}
void IO() {freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}
long long  Factoriel(long long  n) {return n > 1?(n * Factoriel(n-1)):1;}


int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    ll x;cin>>x;
    while(x--){
        ll a;cin>>a;
        string s=to_string(a);ll ans=0;
        ll n=s.length();ll es=0;
        for (ll i = 0; i < n; i++) {


            es= es * 10 + 1;

            for (ll j = 1; j <= 9; j++) {

                if (es * j <= a) {
                    ans++;
                }
            }
            }
            cout<<ans<<endl;
    }
    return 0;
}
