------
## 最终结论
```python
def beautiful_numbers_count(n):
    count = 0
    for i in range(1, 10):  # For each digit from 1 to 9
        beautiful_number = i
        while beautiful_number <= n:
            count += 1
            beautiful_number = beautiful_number * 10 + i  # Create the next beautiful number
    return count

t = int(input())
results = []
for _ in range(t):
    n = int(input())
    results.append(beautiful_numbers_count(n))

print('\n'.join(map(str, results)))
```