#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using std::map;
using std::multiset;
using std::queue;
int t, Ans;
long long n;
inline int get(int x) {
  long long a = x;
  int ans = 0;
  for (int i = 1; a <= n; i++) {
    ans++;
    a *= 10;
    a += x;
  }
  return ans;
}
int main() {
  scanf("%d", &t);
  while (t--) {
    Ans = 0;
    scanf("%lld", &n);
    for (int i = 1; i <= 9; i++) Ans += get(i);
    printf("%d\n", Ans);
  }
}
