#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a[200], s = 0;
  for (long long i = 1, n = 10; n <= 1e9; i += n, n *= 10) {
    for (long long j = i; j <= 9 * i; j += i) {
      a[s] = j;
      s++;
    }
  }
  a[s] = 0x3f3f3f3f;
  int t;
  long long n;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    cin >> n;
    for (long long j = 0; j < 200; j++) {
      if (a[j] > n) {
        cout << j << endl;
        break;
      }
    }
  }
}
