
// Problem: B. Ordinary Numbers
// Contest: Codeforces - Codeforces Round #719 (Div. 3)
// URL: http://codeforces.com/contest/1520/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

int main()
{
	int t; cin>>t;
	while(t--)
	{
		long long n; cin>>n;
		int cnt=0;
		for(long long i=1;i<=9;i++)
		{
			long long val=i;
			while(val<=n)
			{
				cnt++;
				val=10*val+i;
			}
		}
		cout<<cnt<<endl;
	}
	return(0);
}