#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
using ll = long long;
using ld = long double;
using str = string;

#define sz(x) ((int)((x).size()))
#define all(x) (x).begin(), (x).end()

#define EB emplace_back
#define PB push_back
#define LB lower_bound
#define UB upper_bound

#define MP make_pair
#define F first
#define S second

const int dI[4] = {-1, 0, 1, 0};
const int dJ[4] = {0, 1, 0, -1};

const ll MOD = 1e9 + 7;
const ll INF = 2e9;
const ll N = 2e5 + 4;

void solve() {
	int n; cin >> n;
	int ans = 0;
	for (int i = 1; i <= 9; ++i) {
		int j = i;
		while (j <= n) {
			++ans;
			j *= 10; j += i;
		}
	}
	cout << ans << '\n';
}

int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int t; cin >> t; while (t--) solve();
	return 0;
}
