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

int main() {
	#ifdef LOCALFLAG 
		freopen("input5.txt", "r", stdin);
		freopen("output4.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	cout.tie(NULL);
	
	int t ;
	cin >> t;
	//cout << "ji";
	ll n ;
	for(int i =0 ; i < t ; i++){
		cin >> n ;
		ll c = 0;
		ll te = n;
		while(n!=0){
			n/=10;
			c++;
		}
		ll m = te/(pow(10,(c-1)));
		int ca = 1;
		ll ch = 0;
		for(int j = 0 ; j < c ; j++){
			ch+=(ca*m);
			ca*=10;
		}
		ll ans = 0;
		for(int j = 0 ; j < c-1 ; j++){
				ans+=9;
			}
			ans+=(m-1);
		if(ch>te){
			cout << ans <<"\n";
		}
		else{
			cout << (ans+1)<<"\n";
		}
		

	}
  return 0;
}