#include<iostream>
#include<vector>
#include<algorithm>

int main(){
	int n;
	std::vector<int> x;
	std::cin>>n;
	for(int i=1;i<=n;i++){
		int in;
		std::cin>>in;
		x.push_back(in);
	}
	for(int i=0;i<n;i++){
		std::vector<int> s;
		for(int j=1;j<=10000000;j=j*10)s.push_back(x[i]/j%10);
		std::sort(s.begin(),s.end());
		std::cout<<(s[7]-s[0])*10000000+(s[6]-s[1])*1000000+(s[5]-s[2])*100000+(s[4]-s[3])*10000+(s[3]-s[4])*1000+(s[2]-s[5])*100+(s[1]-s[6])*10+(s[0]-s[7])<<std::endl;
		s.clear();
	}
}