#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define re(n) cin >> n
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define ff(i,x,n) for(int i=(int)x;i<(int)n;i++)
#define f(i,n) ff(i,0,n)
#define ffi(i, n, x) for(int i=(int)n; i>=(int)x; i--)
#define fi(i, n) ffi(i, n, 0)
#define rea(a) f(i, (sizeof a/sizeof a[0])){ re(a[i]); }
#define rev(a) f(i, a.size()){ re(a[i]); }
#define puta(a) f(i, (sizeof a/sizeof a[0])){ cout << a[i] << " "; } cout << "\n"
#define putv(a) f(i, a.size()){ cout << a[i] << " "; } cout << "\n"
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define te(t) int t; re(t); while(t--)
#define mod 1000000007

#ifdef LOCAL
    #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" - ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif

ll count_same_digit(ll L, ll R)
{
    ll tmp = 0, ans = 0;
    ll n = log10(R) + 1;
  
    for (ll i = 0; i < n; i++) {
        tmp = tmp * 10 + 1;
        for (ll j = 1; j <= 9; j++) {
  
            if (L <= (tmp * j)
                && (tmp * j) <= R) {
                ans++;
            }
        }
    }
    return ans;
}

int main(){
    fast;
    te(t){
        ll n;
        re(n);
        ll ans = count_same_digit(1, n);
        cout << ans << "\n";
    }
    return 0;
}