#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int64 solve(int64 x) {
  int64 y = x;
  int64 d{1}, p{10};
  while (y >= 10) {
    y /= 10;
    d++;
    p *= 10;
  }
  p = (p - 1) / 9 * y;
  return 9ll * (d - 1) + y - (p > x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int64 n;
    cin >> n;
    cout << solve(n) << '\n';
  }
}
