#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n;
scanf("%d",&n);
int ret = n;
int cnt = 0;
int x, y = 0;
while (ret)
{
if (ret < 10)
x = ret;
ret /= 10;
cnt++;
}
int tnt = cnt;
while (tnt--)
y = y * 10 + x;
// cout << cnt << " " << x << " " << y << "\n";
if (n < 10)
printf("%d\n",n+1);
else
printf("%d\n",(cnt - 1) * 10 + (n >= y) + x - 1);
}
return 0;
}