#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long co = 0;
    long long n;
    cin >> n;
    for (int i = 1; i <= 9; i++) {
      long long rr = 0;
      long long tt = 1;
      rr += tt * i;
      while (rr <= n) {
        co++;
        tt *= 10;
        rr += tt * i;
      }
    }
    cout << co << "\n";
  }
}
