#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
long long power(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2)
    return (power(a, b - 1) * a);
  else {
    long long t = power(a, b / 2);
    return (t * t);
  }
}
bool prime(int a) {
  if (a == 2) return true;
  for (int i = 2; i * i <= a; ++i)
    if (a % i == 0) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long cnt = 0;
    for (int i = 1; i <= 10; ++i) {
      for (int j = 1; j <= 9; ++j) {
        long long h = 0;
        for (int k = i; k > 0; --k) h += (j * pow(10, k - 1));
        if (h <= n) cnt++;
      }
    }
    cout << cnt << "\n";
    ;
  }
  return 0;
}
