#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N=1e5+9;
const int M=4e5+9;
bool vis[N];
int prime[N];
int cnt=0;
void P()
{
vis[1]=1;
for(int i=2;i<N;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
for(int j=i+i;j<N;j+=i) vis[j]=1;
}
}
}
ll power(ll x,ll n)
{
ll ans=1;
while(n)
{
if(n&1) ans=ans*x;
n>>=1;
x=x*x;
}
return ans;
}
ll dp[109];
ll a[109];
ll n;
ll dfs(int pos,bool limit)
{
if(pos==-1) return 1;
if(!limit&&dp[pos]!=-1) return dp[pos];
int up=limit?a[pos]:9;
ll tmp=0;
for(int i=0;i<=up;i++)
{
if(i==7) continue;
tmp+=dfs(pos-1,limit&&i==a[pos]);
}
if(!limit) dp[pos]=tmp;
return tmp;
}
ll solve(ll x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return dfs(pos-1,1);
}
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
int T;
cin>>T;
while(T--)
{
cin>>n;
mem(dp,-1);
printf("%lld\n",solve(n)-1);
}
return 0;
}