#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define all(c) c.begin(), c.end()
int main(){
    int n; cin >> n;
    while (n--){
        string s; cin >> s;
        sort(all(s));
        int n = stoi(s);
        reverse(all(s));
        int m = stoi(s);
        cerr << m << " " << n << endl;
        cout << m - n << endl;
    }
}