#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, ans = 0, count = 0;
    cin >> n;
    set<int> s;
    for (int i = 1; i <= 9; i++) {
      ans = ans * 10 + 1;
      for (int j = 1; j <= 9; j++) {
        if (ans * j <= n) {
          count++;
        } else {
          break;
        }
      }
    }
    cout << count << endl;
  }
}
