#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long pmd(long long a, long long b) {
  if (b == 1) return (a + md) % md;
  if (b % 2 == 0)
    return (pmd(a * a, b / 2) + md) % md;
  else
    return (a * pmd(a * a, b / 2) + md) % md;
}
const int xn = 2e5 + 10;
const int xk = 10;
int f(int r, int d) {
  int g = 0;
  for (int i = 0; i < r; i++) g = g * 10 + d;
  return g;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n;
    if (n == 1e9) {
      cout << 81 << '\n';
      continue;
    }
    m = n;
    int r = 0;
    int mn = 11;
    while (m > 0) {
      mn = min(mn, m % 10);
      m /= 10;
      r++;
    }
    int ans = 9 * r - 9;
    for (int i = 1; i <= 9; i++)
      if (f(r, i) <= n) ans++;
    cout << ans << '\n';
  }
}
