#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, t, i, ans, x, y;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    ans = 0;
    x = n;
    y = 0;
    while (x > 0) {
      y++;
      x /= 10;
    }
    ans += (y - 1) * 9;
    x = 0;
    for (i = 1; i <= y; i++) x = x * 10 + 1;
    for (i = 1; i <= 9; i++)
      if (x * i <= n) ans++;
    printf("%lld\n", ans);
  }
  return 0;
}
