#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ins insert
#define eb emplace_back
//#define endl '\n'
#define ll long long
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int,int>
#define tc int t; cin >> t; while (t--)
#define sz(c) (int) (c).size()
#define trav(x,c) for(auto &x : c)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define _for(i,a,n) for(int i = a ; i < n; ++i)
#define _rfor(i,a,n) for(int i = n-1 ; i >= a; --i)
#define db(x) cout << #x << ": " << x << endl;
#define _db(c) trav(x,c) cout << x << " "; cout << endl;
#define godspeed ios::sync_with_stdio(0); cin.tie(0);

int arr[102030],brr[102030];
void solve(){
        int n;
        cin >> n;
        int cnt=0;
        for(int i=0;i<81;i++){
                if(arr[i]<=n){
                        cnt+=1;
                }
        }
        cout << cnt << '\n';
}
int main() {
        godspeed;
        for(int i=0;i<9;i++){
                arr[i]=i+1;
        }
        for(int i=1;i<=9;i++){
                for(int j=0;j<9;j++){
                        arr[i*9+j]=arr[i*9+j-9]*10+j+1;
                }
        }
        int t=1;cin >> t;
//      read(t);
        while(t--)
        {
                solve();
        }
}