#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  for (long long int i = 0; i < t; i++) {
    long long int n;
    cin >> n;
    if (n / 10 == 0)
      cout << n;
    else if (n / 100 == 0) {
      long long int k = n / 11;
      cout << k + 9;
    } else if (n / 1000 == 0) {
      long long int k = n / 111;
      cout << k + 18;
    } else if (n / 10000 == 0) {
      long long int k = n / 1111;
      cout << k + 27;
    } else if (n / 100000 == 0) {
      long long int k = n / 11111;
      cout << k + 36;
    } else if (n / 1000000 == 0) {
      long long int k = n / 111111;
      cout << k + 45;
    } else if (n / 10000000 == 0) {
      long long int k = n / 1111111;
      cout << k + 54;
    } else if (n / 100000000 == 0) {
      long long int k = n / 11111111;
      cout << k + 63;
    } else if (n / 1000000000 == 0) {
      long long int k = n / 111111111;
      cout << k + 72;
    } else
      cout << 81;
    cout << endl;
  }
  return 0;
}
