#include <bits/stdc++.h>
using namespace std;
long long int cnt(long long int n) {
  long long int ye = 0, last, kop = n;
  vector<long long int> v;
  while (n != 0) {
    ye++;
    long long int mod = n % 10;
    n /= 10;
    last = mod;
  }
  long long int ans = (ye - 1) * 9;
  ans += last - 1;
  for (long long int i = 0; i < ye; i++) {
    v.push_back(last);
    last *= 10;
  }
  long long int sum = 0;
  for (long long int i = 0; i < v.size(); i++) sum += v[i];
  if (sum <= kop) ans++;
  return ans;
}
void solve(long long int n) {
  long long int dig = cnt(n);
  printf("%lld\n", dig);
}
int main() {
  long long int tc;
  scanf("%lld", &tc);
  while (tc--) {
    long long int n;
    scanf("%lld", &n);
    solve(n);
  }
}
