#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
	    long long a,sum=0,b=4,o=5;
	    int he=0;
	    cin>>a;
	    while(b<a){
	    	b+=9*b+4;
	    	he++;
		}
	    //cout<<b<<" "<<he<<" ";
	    for(int j=0;j<he-1;j++){
	    	sum+=o;
	    	o=o*10+5;
		}
		o=(o-5)/10;
		//cout<<sum<<" "<<o<<endl;
		if(a>(b-4)/10&&a<(b-4)/10+o)cout<<a-(b-4)/10+sum-o<<endl;//
		else cout<<sum<<endl;
	}
}