#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
long long factorial(long long n) {
  if (n == 0) return 1;
  return (n * factorial(n - 1)) % 1000000007;
}
long long isPalindrome(string &str) {
  long long diff = 0;
  long long j = str.length() - 1;
  for (long long i = 0; i < j; i++, j--) {
    if (str[i] != str[j]) {
      diff++;
    }
  }
  return diff;
}
void solve() {
  long long n;
  cin >> n;
  long long b = 0, ans = 0;
  for (long long len = 1; len <= 9; len++) {
    b = b * 10 + 1;
    for (long long m = 1; m <= 9; m++)
      if (b * m <= n) ans++;
  }
  cout << ans << endl;
}
int32_t main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
