#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <functional>

using namespace std;

int main(void){
  char c;
  int n, maxNum, minNum;
  vector<int> v;

  cin >> n;
  for(int i=0; i<n; i++){
    v.clear();  maxNum = 0;  minNum = 0;

    for(int j=0; j<8; j++){
      cin >> c;
      v.push_back(c - '0');
    }

    sort(v.begin(), v.end());
    for(int j=0; j<8; j++){
      maxNum += v[j] * pow(10, j);
    }
    sort(v.begin(), v.end(), greater<int>());
    for(int j=0; j<8; j++){
      minNum += v[j] * pow(10, j);
    }

    cout << maxNum - minNum << endl;
  }

  return 0;
}