#include <bits/stdc++.h>
using namespace std;
int main() {
  long int t;
  cin >> t;
  while (t--) {
    long long int n, c = 0, i, j, b = 0;
    cin >> n;
    for (int i = 1; i < 10; i++) {
      b = b * 10 + 1;
      for (j = 1; j <= 9; j++) {
        if (b * j <= n) c++;
      }
    }
    cout << c << endl;
  }
  return 0;
}
