#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);                    \
  cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define ld long double
#define endl "\n"
#define ff first
#define ss second
#define pi pair<ll, ll>
#define PI 3.141592653589793238462643383279502884L
#define mp make_pair
#define pb push_back
#define fall(i, a, b) for (int i = a; i < b; i++)
#define sz(x) (ll) x.size()
#define all(x) x.begin(), x.end()
#define ed cout << "ed";
#define time_passed 1.0 * clock() / CLOCKS_PER_SEC
ll MOD = 1e9 + 7;
ll MOD1 = 1e9 + 123;
ll MOD2 = 1e9 + 321;

int main()
{
  fio;
  ll t, a, n, q, s, k, l, i, m, r, c, u, f, j, p, x, y, b, d;
  c = 0;
  vector<int>V;
  int v[] = {1,11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111};
  for(i=1;i<10;i++)
  for(j=0;j<9;j++)
  V.pb(v[j]*i);
  sort(all(V));
  cin >> t;
  while (t--)
  {
    cin >> n;
    cout << (upper_bound(all(V),n)-V.begin())<< endl;
  }
  return 0;
}
