#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long a;
  scanf("%lld", &a);
  int ans = 0;
  for (int i = 1; i <= 9; i++) {
    long long buf = i;
    while (buf <= a) {
      ans++;
      buf = buf * 10 + i;
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
