#include<bits/stdc++.h>
//#define int long long

using namespace std;

long long inf = 1e18;

long long C(long long n2, long long n3, long long n4, long long n5, long long n6, long long n7, long long n8, long long n9) {
  n8 = n2 / 3;
  n2 %= 3;
  n9 = n3 / 2;
  n3 %= 2;
  n4 = n2 / 2;
  n2 %= 2;
  long long c = n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9;
  if (c >= 18)
    return -1;
  long long r = 0;
  for (long long i = 0; i < n2; i++)
    r = r * 10 + 2;
  for (long long i = 0; i < n3; i++)
    r = r * 10 + 3;
  for (long long i = 0; i < n4; i++)
    r = r * 10 + 4;
  for (long long i = 0; i < n5; i++)
    r = r * 10 + 5;
  for (long long i = 0; i < n6; i++)
    r = r * 10 + 6;
  for (long long i = 0; i < n7; i++)
    r = r * 10 + 7;
  for (long long i = 0; i < n8; i++)
    r = r * 10 + 8;
  for (long long i = 0; i < n9; i++)
    r = r * 10 + 9;
  return r;
}

long long A(long long n2, long long n3, long long n5, long long n7) {
  long long on2 = n2, on3 = n3, on5 = n5, on7 = n7;
  long long n4, n6 = 0, n8, n9;
  long long c = n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9;
  //if (c >= 18)
  //  return -1;
  long long r = inf;
  for (long long i = 0; i <= min(n2, n3); i++) {
    long long cur = C(n2 - i, n3 - i, 0, n5, i, n7, 0, 0);
    if (cur != -1)
      r = min(cur, r);
  }
  if (r == inf)
    r = -1;
  if (r == 0)
    r = 1;
  //if (r < 0) {
  //if ((on2 == 1 and on3 == 2 and on5 == 0 and on7 == 0)) {
  //  cout << on2 << " " << on3 << " " << on5 << " " << on7 << endl;
  //  cout << n2 << " " << n3 << " " << n4 << " " << n5 << " " << n6 << " " << n7 << " " << n8 << " " << n9 << endl;
  //  cout << r << endl;
  //}
  return r;
}

long long B(long long n) {
  set<long long> s;
  for (long long i = 1; i <= n; i++) {
    long long x = i, r = 1;
    while (x > 0) {
      r *= (x % 10);
      x /= 10;
    }
    s.insert(r);
  }
  return s.size();
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long m2 = 54, m3 = 36, m5 = 18, m7 = 18;
  vector<long long> v;
  for (long long n2 = 0; n2 <= m2; n2++) {
    for (long long n3 = 0; n3 <= m3; n3++) {
      for (long long n5 = 0; n5 <= m5; n5++) {
        for (long long n7 = 0; n7 <= m7; n7++) {
          long long r = A(n2, n3, n5, n7);
          if (r != -1)
            v.push_back(r);
        }
      }
    }
  }
  sort(v.begin(), v.end());
  //cout << v[0] << " " << v[1] << " " << v[2] << endl;
  map<long long, bool> m;
  for (long long x : v) {
    if (x > 50)
      break;
    //cout << x << " ";
    long long r = 1;
    while (x != 0) {
      r *= x % 10;
      x /= 10;
    }
    //if (m[r])
    //  cout << "AAAAAAAAAAAAAA" << endl;
    m[r] = true;
    //cout << r << endl;
  }
  long long t;
  cin >> t;
  //t = 1000;
  while (t--) {
    long long n = t, b = 0;
    cin >> n;
    if (n >= 10)
      b++;
    b += upper_bound(v.begin(), v.end(), n) - v.begin();
    //if (B(n) != b) {
    //  cout << n << " " << b << " " << B(n) << endl;
    //}
    cout << b << "\n";
  }
  return 0;
}