n = int(input())
for i in range(n):
a = int(input())
b = a//10
if b == 0:
print(a)
else:
c = len(str(b))*9
d = a//int(int(len(str(a)) * '1'))
print(d+c)
#include <bits/stdc++.h>
#define ll long long
#define db double
#define str string
#define vi vector<int>
#define vll vector<ll>
#define maax(a,b,c) max(max(a,b),c)
#define diff(a,b) abs(a-b)
#define pi 2*asin(1)
#define pb push_back
#define mp make_pair
#define sfi(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sfc(a) scanf("%ch",&a)
#define pf printf
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define iobost ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define el '\n'
#define sp ' '
//318424JO timus judge id
using namespace std;
int main()
{
iobost;
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int ans=0;
for(int power=1;power<=n;power=power*10+1)
{
for(int d=1;d<10;d++)
{
if(d*power<=n)ans++;
}
}
cout<<ans<<el;
}
return 0;
}
151A - Soft Drinking | 1352A - Sum of Round Numbers |
281A - Word Capitalization | 1646A - Square Counting |
266A - Stones on the Table | 61A - Ultra-Fast Mathematician |
148A - Insomnia cure | 1650A - Deletions of Two Adjacent Letters |
1512A - Spy Detected | 282A - Bit++ |
69A - Young Physicist | 1651A - Playoff |
734A - Anton and Danik | 1300B - Assigning to Classes |
1647A - Madoka and Math Dad | 710A - King Moves |
1131A - Sea Battle | 118A - String Task |
236A - Boy or Girl | 271A - Beautiful Year |
520B - Two Buttons | 231A - Team |
479C - Exams | 1030A - In Search of an Easy Problem |
158A - Next Round | 71A - Way Too Long Words |
160A - Twins | 1A - Theatre Square |
1614B - Divan and a New Project | 791A - Bear and Big Brother |