#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string a;
    cin >> a;
    sort(a.begin(), a.end());
    int min_a = stoi(a);
    reverse(a.begin(), a.end());
    int max_a = stoi(a);
    cout << max_a - min_a << endl;
  }
}
