#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<set>
#include<fstream>
constexpr int Mod = 998244353;
using namespace std;


int t;
int n;



int main() {
    cin >> t;




    while (t--) {
        cin >> n;
        int ans = 0;
        int p = 10;
        int x = 1;
        while (p < n) {
            for (int i = 1; i <= 9; i++) {
                if (x * i <= n) {
                    ans++;
                }
            }
            x = x * 10 + 1;
            p = p * 10;
        }
        for (int i = 1; i <= 9; i++) {
            if (x * i <= n) {
                ans++;
            }
        }
        cout << ans << endl;
    }


    return 0;
}