#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};

int main()
{
  priority_queue<int> Q;
  priority_queue<int,vector<int>, greater<int> > P;
  int saidai,saisyo;
  int n;
  cin >>n;
  for(int i = 0;i < n;i++){
    saidai = 0;
    saisyo = 0;
    string a;
    cin >>a;
    for(int i = 0;i <a.size();i++){
      int b = a[i]-'0';
      Q.push(b);
      P.push(b);
    }
    for(int i = 1;i <=8;i++){
      saidai +=Q.top()*(pow(10.0,8-i));
      saisyo +=P.top()*(pow(10.0,8-i));
      Q.pop();
      P.pop();
  }
    cout <<saidai-saisyo<<endl;
  }
  return 0;
}