#include <bits/stdc++.h>
using namespace std;
void speed() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long int PowerOf2Num(long long int n) { return (n & (~(n - 1))); }
bool isPow(long long int x) { return x && (!(x & (x - 1))); }
bool divByDig(long long int num) {
  long long int x = num;
  while (x > 0) {
    long long int y = x % 10;
    x /= 10;
    if (y != 0 && num % y != 0) {
      return false;
    }
  }
  return true;
}
long long int numDig(long long int num) {
  long long int dig = 0;
  while (num > 0) {
    dig++;
    num /= 10;
  }
  return dig;
}
bool sameDig(long long int num) {
  long long int x = num;
  long long int same = x % 10;
  x /= 10;
  while (x > 0) {
    long long int yo = x % 10;
    if (same != yo) {
      return false;
    }
    x /= 10;
  }
  return true;
}
string decToBin(long long int n) {
  string s = "";
  for (long long int i = 63; i >= 0; i--) {
    long long int k = n >> i;
    if (k & 1) {
      s += "1";
    } else {
      s += "0";
    }
  }
  return s;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int b = 0, count = 0;
    for (long long int len = 1; len <= 9; len++) {
      b = b * 10 + 1;
      for (long long int m = 1; m <= 9; m++)
        if (b * m <= n) {
          count++;
        }
    }
    cout << count << '\n';
  }
}
