#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

int pow(int num, int count){
	while(count>0){
		num*=num;
		count --;
	}
	return num;
}

int main(void){
	stringstream ss;
	int n;
	while(cin >> n){
		for(int i=0;i<n;i++){
			string s;
			cin >> s;
			for(int j=0;j<8;j++){
				for(int k=j+1;k<8;k++){
					if((int)s[j]>(int)s[k]){
					int tmp = s[j];
					s[j] = s[k];
					s[k] = tmp;
					}
				}
			}
			cout << "#" << s << "\n";
			int min,max;
			ss << s << " ";
			reverse(s.begin(),s.end());
			ss << s;
			ss >> min >> max;
			cout << max - min << "\n";
		}
	}
	return 0;
}