#include <iostream>
using namespace std;

int main () {

    int test;
    cin>>test;

    for (int t = 1; t <= test; t++) {

        int n;
        cin>>n;

        int count = 0;
        for (int i = 1; i <= n; i = i*10+1) {
            for (int j = 1; j <= 9; j++) {
                if (i * j <= n) {
                    count++;
                }
            }
        }

        cout<<count<<endl;
    }
}