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

void solve(){

	int n;
	cin >> n;
	int count = 0;
	for (int d = 1; d <= 9; d++) {
		for (int x = 1; x <= 9; x++){
			count += stoi(string(d, '0' + x)) <= n;
		}
	}
	cout << count << '\n';
}

int main(){
	ios::sync_with_stdio(0); cin.tie(nullptr);
	int tt;
	cin >> tt;
	while(tt--) solve();
}