#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
unsigned long long power(long long x, unsigned long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
vector<long long> z_function(string s) {
  long long n = (long long)s.length();
  vector<long long> z(n);
  for (long long i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r) z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
    if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
  }
  return z;
}
long long cnt[10];
string res;
string s;
bool ans(long long l, long long k) {
  if (l == 0 && k == 0) {
    res.clear();
    for (long long i = 0; i < s.length() - 2; i++) res += '9';
    return 1;
  }
  cnt[k]++;
  long long t = 0;
  for (long long i = 0; i < 10; i++) t += (cnt[i] % 2);
  if (l + t + 1 > s.size()) {
    cnt[k]--;
    return 0;
  }
  res = "";
  for (long long i = 0; i < l; i++) res += s[i];
  res += char(k + '0');
  vector<char> v;
  l++;
  for (long long i = 9; i >= 0; i--)
    if (cnt[i] % 2) {
      v.push_back(char(i + '0'));
      l++;
    }
  while (l != s.size()) {
    v.push_back('9');
    l++;
  }
  sort(v.begin(), v.end());
  for (long long i = (long long)v.size() - 1; i >= 0; i--) res += v[i];
  return 1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  ;
  while (t--) {
    cin >> s;
    long long n = s.length();
    memset(cnt, 0, sizeof(cnt));
    for (long long i = 0; i < n; i++) cnt[s[i] - '0']++;
    bool ok = false;
    for (long long i = n - 1; !ok && i >= 0; i--) {
      cnt[s[i] - '0']--;
      for (long long k = s[i] - '0' - 1; k >= 0; k--)
        if (ans(i, k)) {
          ok = 1;
          break;
        }
    }
    bool flag = 1;
    for (long long i = 0; i < res.size(); i++) {
      if (flag && res[i] == '0') continue;
      flag = 0;
      cout << res[i];
    }
    cout << "\n";
  }
}
