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

typedef long long ll;
const ll inf = 1LL << 60;

vector<ll> nums;

void gen() {
    for(int i = 1; i <= 9; i++) {
        ll t = i;

        for(int j = 0; j < 10; j++) {
            nums.push_back(t);

            t *= 10;
            t += i;
        }
    }
}

void solve() {
    ll n;
    cin >> n;

    int ans = 0;

    for(ll it: nums) {
        if(it <= n) ans++;
    }

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    gen();

    int tc = 1;
    cin >> tc;

    while(tc--) {
        solve();
    }
}
