#include <iostream>
#include <string> 
using namespace std;

int main () {
    int input[10000],output[10000],cases,iL,oL;
	long double mul,rem;
	string tmp;
	cin >> cases;
	for(int i=0;i<cases;i++) {
		cin >> tmp;
		iL = 0; oL = 0;
		if(tmp.length()%2==1) {
			input[0] = tmp[0]-48;
			iL++;
			for(int j=1;j<tmp.length();iL++,j++) {
				input[iL] = tmp[j]-48;
				input[iL] = input[iL]*10 + (tmp[j+1]-48);
				j++; 
			}
		}
		else {
			for(int j=0;j<tmp.length();iL++,j++) {
				input[iL] = tmp[j]-48;
				input[iL] = input[iL]*10 + (tmp[j+1]-48);
				j++; 
			}
		}
		//for(int j=0;j<iL;j++) cout << input[j] << " "; //check input
		mul = 0; rem = 0;
		for(int j=0;j<iL;j++) {
			rem = rem*100 + input[j];
			for(int k=9;k>=0;k--) {
				if((mul*10+k)*k <= rem) {
					rem = rem - (mul*10+k)*k;
					//cout << "debug : " << mul*10+k << " " << rem << " ";
					mul = mul*10+k*2;
					//cout << mul << endl;
					output[oL] = k;
					oL++;
					break;
				}
			}
		}
		for(int j=0;j<oL;j++) cout << output[j]; //check output
		cout << endl;
	}
}