/* Author: EndlessK
 * Time: 2021-05-06 20:40:25
**/
#include<bits/stdc++.h>
#define maxn 100010
#define pb push_back
#define ll long long
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int main()
{
    fast;
    int t;
    cin>>t;
    map<int,int> mp;
    while(t--)
    {
        int n;
        int ans=0;
        cin>>n;
        for(int i=1;i<=min(9,n);i++)
        {
            ans++;
        }
        for(int i=11;i<=min(99,n);i+=11)
        {
            ans++;
        }
        for(int i=111;i<=min(999,n);i+=111)
        {
            ans++;
        }
        for(int i=1111;i<=min(9999,n);i+=1111)
        {
            ans++;
        }
        for(int i=11111;i<=min(99999,n);i+=11111)
        {
            ans++;
        }
        for(int i=111111;i<=min(999999,n);i+=111111)
        {
            ans++;
        }
        for(int i=1111111;i<=min(9999999,n);i+=1111111)
        {
            ans++;
        }
        for(int i=11111111;i<=min(99999999,n);i+=11111111)
        {
            ans++;
        }
        for(int i=111111111;i<=min(999999999,n);i+=111111111)
        {
            ans++;
        }
        cout<<ans<<'\n';
    }
    return 0;
}