/*

   _____            _            _   ____              __          __ _      
  / ____|          | |          | | |  _ \          _  \ \        / /(_)     
 | |      ___    __| |  ___   __| | | |_) | _   _  (_)  \ \  /\  / /  _  ____
 | |     / _ \  / _` | / _ \ / _` | |  _ < | | | |       \ \/  \/ /  | ||_  /
 | |____| (_) || (_| ||  __/| (_| | | |_) || |_| |  _     \  /\  /   | | / / 
  \_____|\___/  \__,_| \___| \__,_| |____/  \__, | (_)     \/  \/    |_|/___|
                                             __/ |                           
                                            |___/                            

*/
#include<bits/stdc++.h>
using namespace std;
#define ff              first
#define ss              second
#define ll              long long
#define ld              long double
#define vll             vector<ll>
#define vvl             vector<vector<ll>>
#define pll             pair<ll,ll>
#define vpll            vector<pair<ll,ll>>
#define uset            unordered_set
#define pb              push_back
#define show(x)         cout <<(#x)<<" : "<<x<<endl
#define all(x)          x.begin(),x.end()
#define sortall(x)      sort(all(x))
#define sortrev(x)      sort(all(x), greater<ll>())
#define op(x)           cout<<x<<endl
#define opl(x)          cout<<x<<" "
#define MOD             1073741824
#define MAX             1e6


void solve(){
  ll n,cnt = 0;
  cin>>n;
  for(int i=1;i<10;i++){
    ll curr = i;
    while(curr <= n){
      cnt++;
      curr *=10;
      curr+=i;
    }
  }
  op(cnt);
}

int main(){
  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  int t=1;
  cin>>t;
  while(t--) solve();
    return 0;
}
