
//RITIK RAI

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


#define endl '\n'
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define preci(x, y) fixed << setprecision(y) << x

#define vint vector<int>
#define vvint vector<vector<int>>
#define sz(a) (int)a.size()
#define pb push_back
#define vmin *min_element
#define vmax *max_element
#define max_pq priority_queue<ll, vector<ll>>
#define min_pq priority_queue<ll, vector<ll>, greater<ll>>
#define mint map<int,int>
#define pint pair<int,int>
#define mp make_pair
#define F first
#define S second

#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ub upper_bound
#define lb lower_bound
#define bin binary_search
#define revsort(v) sort(all(v), greater<int>())
#define min(a,b) min((int)a,(int)b)
#define max(a,b) max((int)a,(int)b)
#define min3(a,b,c) min(min(a,b),c)
#define max3(a,b,c) max(max(a,b),c)

#define f(i,a,b) for(int i=(a);i<(b);i++)
#define rf(i,a,b) for(int i=(a);i>(b);i--)

#define uint unsigned long long int // uint64_t
#define ll long long int //int64_t
#define ld long double

void _print(ll t) {cout << t;}
void _print(int t) {cout << t;}
void _print(string t) {cout << t;}
void _print(char t) {cout << t;}
void _print(ld t) {cout << t;}
void _print(double t) {cout << t;}
void _print(uint t) {cout << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cout << "{"; _print(p.first); cout << ","; _print(p.second); cout << "}";}
template <class T> void _print(vector <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(set <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(multiset <T> v) {cout << "[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T, class V> void _print(map <T, V> v) {cout << "[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";}
void _Print(ll t) {cerr << t;}
void _Print(int t) {cerr << t;}
void _Print(string t) {cerr << t;}
void _Print(char t) {cerr << t;}
void _Print(ld t) {cerr << t;}
void _Print(double t) {cerr << t;}
void _Print(uint t) {cerr << t;}
template <class T, class V> void _Print(pair <T, V> p);
template <class T> void _Print(vector <T> v);
template <class T> void _Print(set <T> v);
template <class T, class V> void _Print(map <T, V> v);
template <class T> void _Print(multiset <T> v);
template <class T, class V> void _Print(pair <T, V> p) {cerr << "{"; _Print(p.first); cerr << ","; _Print(p.second); cerr << "}";}
template <class T> void _Print(vector <T> v) {cerr << "[ "; for (T i : v) {_Print(i); cerr << " ";} cerr << "]";}
template <class T> void _Print(set <T> v) {cerr << "[ "; for (T i : v) {_Print(i); cerr << " ";} cerr << "]";}
template <class T> void _Print(multiset <T> v) {cerr << "[ "; for (T i : v) {_Print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _Print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_Print(i); cerr << " ";} cerr << "]";}
// #define DEBUG // TRACE 
// #ifdef DEBUG // TRACE
#ifdef RITIK
#define trace(...) __f1(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f1(const char* name, Arg1&& arg1) {
    // cout << name << " : " << arg1 << std::endl;
    cout << name << " : "; _print(arg1); cout<<"\n";
}
template <typename Arg1, typename... Args>
void __f1(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : "; _print(arg1); cout << " | "; __f1(comma + 1, args...);
}
#define Trace(...) __f2(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f2(const char* name, Arg1&& arg1) {
    // cerr << name << " : " << arg1 << std::endl;
    cerr << name << " : "; _Print(arg1); cerr<<"\n";
}
template <typename Arg1, typename... Args>
void __f2(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : "; _Print(arg1); cerr << " | "; __f2(comma + 1, args...);
}
#define debug if(T!=1){cout<<endl<<"-------------------------------"<<endl<<endl<<endl;cerr<<endl<<"-------------------------------"<<endl<<endl<<endl;}cout<<"TEST ::::::: ";cerr<<"TEST ::::::: ";trace(T);Trace(T)
#else
#define trace(...)
#define Trace(...)
#define debug
#endif

void ritikio()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // #ifndef ONLINE_JUDGE
#ifdef RITIK
        freopen("input999.txt", "r", stdin);
        freopen("output999.txt", "w", stdout);
        freopen("error.txt","w",stderr);
        //fclose(stdout);
#endif
}

#define int long long int //int64_t

#define PI 3.1415926535897932384626433832795 // atan(1.0)*4.0;
const int inff = LLONG_MAX;
const int minff = LLONG_MIN;
const int inf = 1e18;
const int pmod = 998244353;
const int mod = 1e9 + 7;
const int maxm = 2e6 + 3;
const int maxN = 1e5 + 3;
int T = 0;






void solve()
{ 
    int n; cin>>n;
    int m=n;
    int len=0;
    while(m>0) len++, m=m/10;
    int ans=9*(len-1);
    f(i,1,10)
    {
        int r=i;
        f(j,1,len) r=r*10+i;
        if(r>n) break;
        ans++;
    }
    cout<<ans<<endl;
}






void reset()
{
}

void precompute()
{
}

signed main()
{
    ritikio();

    //precompute();


    int TT = 1;
    T = 0;
    cin >> TT;

    while(++T < TT + 1)
    {
#ifdef RITIK
        debug;
#endif
        solve();
        // reset();
    }
    
}

