https://codeforces.com/problemset/problem/1277/A
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(void)
{
int n; cin>>n;
while(n--)
{
int x; cin>>x;
int cnt=0;
for(int i=1;i<=9;i++)
{
LL temp=0;
while( (temp*10+i)<=x)
{
cnt++,temp=temp*10+i;
}
}
cout<<cnt<<endl;
}
return 0;
}