#include <iostream>
#include <iomanip>
#include <cassert>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <sstream>
#include <istream>
#include <cmath>
#include <cstdio>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i=(int)a; i<(int)b; i++)
#define for_d(i, a, b) for (int i=(int)a-1; i>=b; i--)
#define for_r(i, a, b, c) for (int i=(int)a; i<(int)b; i += c)
#define for_dr(i, a, b, c) for (int i=(int)a-1; i>=b; i -= c)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

typedef long long ll;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }


ll sti(string s) {
	ll len = s.length();
	if (len < 1) return 0;
	return (s[len - 1] - '0') + 10 * (sti(s.substr(0, len - 1)));
}


int main() {
	vci v(8);
	int N; cin >> N;
	
	for_(p, 0, N) {
		string line; cin >> line;
		int ln = sti(line);
		
		for_(i, 0, 8) {
			v[i] = ln % 10;
			ln /= 10;
		}
		
		sort(all(v));
		int maxi = 0, mini = iINF;
		do {
			int val = 0;
			for_(i, 0, 8) val += v[i] * pow(10, i);
			maxi = max(maxi, val);
			mini = min(mini, val);
		} while(next_permutation(all(v)));
		
		cout << maxi-mini << endl;
	}	
	
	return 0;
}