#include <bits/stdc++.h>
using namespace std;
int main(void){
    std::deque<int> deq;
    int n;
    string str;
    cin >> n;
    for (int i=0;i<n;i++) {
        cin>>str;
        for (int j=0;j<str.size();j++) {
            int hoge = str.at(j)-48;
            deq.emplace_back(hoge);
        }
        sort(deq.begin(),deq.end());
        int min = 10000000*deq[0]+1000000*deq[1]+100000*deq[2]+10000*deq[3]+1000*deq[4]+100*deq[5]+10*deq[6]+1*deq[7];
        sort(deq.begin(),deq.end(),greater<int>());
        int max = 10000000*deq[0]+1000000*deq[1]+100000*deq[2]+10000*deq[3]+1000*deq[4]+100*deq[5]+10*deq[6]+1*deq[7];
        cout<<max-min<<endl;
        deq.clear();
    }
}

