#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const int INF = 2000000000;
const long long LINF = 2000000000000000000;
template <typename T>
void print(vector<T>& a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
}
template <typename T>
void print(deque<T>& a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
}
template <typename T>
void print(vector<vector<T>>& a) {
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++) cout << a[i][j] << ' ';
    cout << '\n';
  }
}
template <typename T>
void input(vector<T>& a) {
  for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(deque<T>& a) {
  for (int i = 0; i < a.size(); i++) cin >> a[i];
}
template <typename T>
void input(vector<vector<T>>& a) {
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < a[i].size(); j++) cin >> a[i][j];
}
const int N = 2 * (1000000 + 10);
struct elem {
  int MAX, MIN;
};
elem make_elem(int val) {
  elem res;
  res.MAX = val;
  res.MIN = val;
  return res;
}
elem combine(elem a, elem b) {
  elem res;
  res.MAX = max(a.MAX, b.MAX);
  res.MIN = min(a.MIN, b.MIN);
  return res;
}
elem t[4 * N];
int tadd[4 * N];
const elem neutral = {-INF, INF};
void push(int v, int vl, int vr) {
  if (tadd[v] != 0) {
    t[v].MAX += tadd[v];
    t[v].MIN += tadd[v];
    if (vl != vr) {
      tadd[2 * v + 1] += tadd[v];
      tadd[2 * v + 2] += tadd[v];
    }
    tadd[v] = 0;
  }
}
elem query(int v, int vl, int vr, int l, int r) {
  push(v, vl, vr);
  if (r < vl || vr < l) return neutral;
  if (vl >= l && vr <= r) return t[v];
  int vm = (vl + vr) / 2;
  elem ql = query(2 * v + 1, vl, vm, l, r);
  elem qr = query(2 * v + 2, vm + 1, vr, l, r);
  return combine(ql, qr);
}
void modify(int v, int vl, int vr, int l, int r, int val) {
  push(v, vl, vr);
  if (r < vl || vr < l) return;
  if (vl >= l && vr <= r) {
    tadd[v] = val;
    push(v, vl, vr);
    return;
  }
  int vm = (vl + vr) / 2;
  modify(2 * v + 1, vl, vm, l, r, val);
  modify(2 * v + 2, vm + 1, vr, l, r, val);
  t[v] = combine(t[2 * v + 1], t[2 * v + 2]);
}
void get_ans(int ptr) {
  elem res = query(0, 0, N - 1, 0, N - 1);
  if (res.MIN < 0)
    cout << -1 << ' ';
  else {
    elem res2 = query(0, 0, N - 1, N - 1, N - 1);
    if (res2.MAX == 0)
      cout << res.MAX << ' ';
    else
      cout << -1 << ' ';
  }
}
vector<char> a;
void modif(int ptr, char c) {
  if (a[ptr] == c) return;
  if (c != ')' && c != '(') {
    if (a[ptr] != '(' && a[ptr] != ')') {
      a[ptr] = c;
      return;
    }
    if (a[ptr] == '(') {
      modify(0, 0, N - 1, ptr, N - 1, -1);
      a[ptr] = c;
    } else {
      modify(0, 0, N - 1, ptr, N - 1, 1);
      a[ptr] = c;
    }
    return;
  } else {
    if (c == '(') {
      if (a[ptr] == ')') {
        modify(0, 0, N - 1, ptr, N - 1, 2);
        a[ptr] = c;
      } else {
        modify(0, 0, N - 1, ptr, N - 1, 1);
        a[ptr] = c;
      }
    } else {
      if (a[ptr] == '(') {
        modify(0, 0, N - 1, ptr, N - 1, -2);
        a[ptr] = c;
      } else {
        modify(0, 0, N - 1, ptr, N - 1, -1);
        a[ptr] = c;
      }
    }
  }
}
void solve() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      int t = 0;
      int p = 1;
      for (int k = 0; k < i; k++) {
        t += p * j;
        p *= 10;
      }
      if (t <= n) ans++;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tst = 1;
  cin >> tst;
  while (tst--) solve();
}
