#include <bits/stdc++.h>
using namespace std;
int main() {
  int TestCase;
  int BeautifulYear[83] = {1};
  for (int Z = 1, Add = 1, AddTen = 1; Z < 81; Z++) {
    if (Z % 9 == 0) {
      AddTen = AddTen * 10;
      BeautifulYear[Z] = BeautifulYear[Z - 9] + AddTen;
      Add = BeautifulYear[Z];
      continue;
    }
    BeautifulYear[Z] = BeautifulYear[Z - 1] + Add;
  }
  cin >> TestCase;
  for (int Q = 0, Year; Q < TestCase; Q++) {
    cin >> Year;
    for (int Z = 0; Z < 81; Z++) {
      if (Z == 80) {
        cout << Z + 1 << "\n";
        break;
      }
      if (BeautifulYear[Z] < Year && BeautifulYear[Z + 1] > Year) {
        cout << Z + 1 << "\n";
        break;
      } else if (BeautifulYear[Z] == Year) {
        cout << Z + 1 << "\n";
        break;
      }
    }
  }
  return 0;
}
