#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main() {
  int n; cin >> n;
  while (n--) {
    vector<int> v;
    long l; cin >> l;
    while (l != 0) { v.push_back(l % 10); l /= 10; }
    sort(v.begin(), v.end());
    long a = 0, b = 0;
    for (auto it = v.begin(); it != v.end(); it++) a = a * 10 + *it;
    for (auto it = v.rbegin(); it != v.rend(); it++) b = b * 10 + *it;
    cout << b - a << endl;
  }
}