#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  int n;
  cin >>n;
  while(n--){
    vector<int> a(8);
    for(int i=0; i<8; i++) scanf("%1d",&a[i]);
    sort(a.begin(),a.end());
    int m = 0,M = 0;
    for(int i=0; i<8; i++){
      m = m*10+a[i];
      M = M*10+a[7-i];
    }
    cout <<M-m<<endl;
  }
  return 0;
}