#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int Case = 1;
ll dp[20][2], num[20];
ll dfs(int len, int f4, int f2) {
	if(!len) return 1;
	else if(!f2 && dp[len][f4]) return dp[len][f4];
	ll res = 0, up = f2?num[len]:9;
	for(int i  = 0; i <= up; i++) {
		if(f4&&i == 9) continue;
		res += dfs(len-1, i == 4, f2&&(i == up));
	}
	return f2?res:dp[len][f4] = res;
}
ll cal(ll x) {
	int pos = 0;
	while(x) {
		num[++pos] = x%10;
		x /= 10;
	}
	return dfs(pos, 0, 1);
}
void solve() {
    ll n;
    scanf("%lld", &n);
    printf("%lld\n", n+1-cal(n));
    return;
}

int main() {
    scanf("%d", &Case);
    while(Case--) {
        solve();
    }
    return 0;
}