#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, x = 0, i, y = 1, m, a = 1;
    scanf("%d", &n);
    while (1) {
      for (i = 1; i <= 9; i++) {
        if (y * i <= n)
          x++;
        else
          break;
      }
      y = (y * 10) + 1;
      if (y > n) break;
    }
    cout << x << endl;
  }
  return 0;
}
