int main()
 {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);


    minRep[1] = 1;


    for(int d=2;d<=9;d++)
    {
        generate(d, d, d);
    }


    vector<ull> reps;


    for (auto& p : minRep) 
    {
        reps.push_back(p.second);


    }
    sort(reps.begin(), reps.end());

    int T;

    cin>>T;

    while(T--) 
    {
        ull n;
        cin>>n;

        int cnt =upper_bound(reps.begin(),reps.end(), n)-reps.begin();


        if(n>=10) 
        cnt++;

        cout<<cnt <<"\n";
    }

    return 0;
}