#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define REP(i, n) for(int i=0; i<(n); i++)
#define REP2(i, x, n) for(int i=x; i<(n); i++)
#define SORT(n) sort((n).begin(), (n).end())
#define SORT2(n) sort((n).begin(), (n).end(), greater<int>())
using namespace std;
int main(){
	
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n;
	int res1 = numeric_limits<int>::min();
	int res2 = numeric_limits<int>::max();
	cin >> n;
	
	string s;
	REP(i, n){
		cin >>s;
		vector<int> MAX,MIN;
		REP(j, 8){
			string tmp = s.substr( j, 1 );
			int tmp2 = stoi(tmp);
			MAX.push_back(tmp2);
			MIN.push_back(tmp2);
		}
		
		SORT2(MAX);
		SORT(MIN);
		
		string tmp1,tmp2;
		REP(i, 8){
			tmp1+=to_string(MAX[i]);
			tmp2+=(MIN[i] != 0 ? to_string(MIN[i]) : "");
		}

		res1 = max(res1, stoi(tmp1));
		res2 = min(res2, stoi(tmp2));
		
		cout << res1 - res2 << endl;
	}
	return 0;
}