#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int x = 0;
    if (n >= 10) {
      for (int i = 0; i < 10; i++) {
        if (pow(10, i) > n) {
          break;
        } else {
          x++;
        }
      }
      x--;
      int sum = 0;
      sum += 9 * x;
      int y = 1, j = 1;
      while (x--) {
        y += pow(10, j);
        j++;
      }
      sum += n / y;
      cout << sum << "\n";
    } else {
      cout << n << "\n";
    }
  }
  return 0;
}
