模板题
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num;
int a[20],len;
ll dp[2][20],ten[20];
ll dfs(int pos,bool pre,bool lim)
{
int Max=lim?a[pos]:9;
if(pos==1)return Max==9&⪯
if(!lim&&dp[pre][pos]!=-1)return dp[pre][pos];
ll ret=0;
for(int i=0;i<=Max;++i)
{
//printf("%d:%d ",pos,i);
if(i==9&&pre)ret+=lim?(num%ten[pos-1]+1):ten[pos-1];
else ret+=dfs(pos-1,i==4,lim&&i==Max);
//printf("%d
",ret);
}
if(!lim)dp[pre][pos]=ret;
return ret;
}
ll solve(ll x)
{
len=0;
while(x)
{
a[++len]=x%10;
x/=10;
}
return dfs(len,0,1);//todo
}
int main()
{
//freopen("Input.txt","r",stdin);
ten[0]=1;
for(int i=1;i<=19;++i)ten[i]=ten[i-1]*10;
memset(dp,-1,sizeof dp);
int t;scanf("%d",&t);
while(t--)
{
scanf("%I64d",&num);
printf("%I64d
",solve(num));
}
}