#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD 1000000007
#define mod 1000000007
#define  mfor(i,a,b)  for(int i=a;i<=b;i++)
#define p_b  push_back
#define m_p make_pair
#define all(v) v.begin(),v,end()
typedef  long long int ll;
#define sp(x) setprecision(x)
//cout<<fixed<<sp(len_of_decimal)<<ans<<endl;
int dir8[2][8]={{0,0,1,-1,1,1,-1,-1},{1,-1,0,0,1,-1,-1,1}};
int dir4[2][4]={{0,0,1,-1},{1,-1,0,0}};

void solve()
{
    ll n;
    cin>>n;
    vector<ll> gg={1,11,111,1111,11111,111111,1111111,11111111,111111111};
    int ans=0;
    for(int i=0;i<gg.size();i++){
        for(int j=1;j<=9;j++){
            ll num= gg[i]*j;
            if(num<=n)
                ans++;
        }
    }
    cout<<ans<<endl;
    return;
}
int main()
{
   SPEED
   int t;
   cin>>t;
   while(t--)
   solve();
   return 0;
}

