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