#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 1000000007
#define lli long long int
#define li long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl "\n"


int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    lli t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
       int count=0;
        for(lli i=1; i<=n; i=i*10 +1)
        {
            for(int j=1; j<=9; j++)
            {
                if(i*j<=n)
                {
                    count++;
                }
            }
        }
        cout<<count<<"\n";
    }
    return 0;
}