#include <bits/stdc++.h>
using namespace std;
long long t, n;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    long long f = 0, ans = 0;
    for (long long i = 1; i <= 9; i++) {
      f = f * 10 + 1;
      for (long long j = 1; j <= 9; j++) {
        long long m = f * j;
        if (m <= n) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
