#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int n;
    cin >> n;
    for(int i=0; i<n; ++i) {
        string s1, s2;
        cin >> s1;
        s2 = s1;
        sort(s1.begin(), s1.end());
        sort(s2.rbegin(), s2.rend());
        cout << stoi(s2) - stoi(s1) << endl;
    }
}
