#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
char t;
vector<int> a;

int main(){
	cin>>n;
	rep(i,n){
		ll MIN=0,MAX=0;
		a.clear();
		rep(j,8){
			cin>>t;
			//cout<<t-'0'<<endl;
			int t1=t-'0';
			//cout<<t1<<endl;
			a.PB(t1);
			
		}
		
		sort(a.begin(),a.end());
		ll tmp=1;
		//rep(k,8) cout<<a[k]<<" ";
		//cout<<endl;
		rep(k,8){
			if(k==0) tmp=1;
			else tmp*=10;
			MIN+=a[7-k]*tmp;
			MAX+=a[k]*tmp;
			//cout<<MAX<<" "<<MIN<<endl;
		}
		cout<<MAX-MIN<<endl;
	}
	return 0;
}