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