#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    string second;
    cin >> second;
    int n = second.size();
    int first = -1;
    int e = -1;
    int mask = 0;
    for (int i = 0; i < n; ++i) {
      for (int k = 1; k < 10; ++k) {
        if ((i != 0 && second[i] - k >= '0') ||
            (i == 0 && second[i] - k >= '1')) {
          if ((n - i - 1 -
               __builtin_popcount(mask ^ (1 << (second[i] - '0' - k)))) %
                      2 ==
                  0 &&
              n - i - 1 -
                      __builtin_popcount(mask ^ (1 << (second[i] - '0' - k))) >=
                  0) {
            first = i;
            e = k;
            break;
          }
        }
      }
      mask ^= (1 << (second[i] - '0'));
    }
    if (first == -1) {
      for (int i = 2; i < second.size(); ++i) cout << '9';
    } else {
      mask = 0;
      for (int i = 0; i < first; ++i) {
        cout << second[i];
        mask ^= (1 << (second[i] - '0'));
      }
      mask ^= (1 << (second[first] - '0' - e));
      cout << char(second[first] - e);
      int res = n - first - 1;
      int cnt = res - __builtin_popcount(mask);
      for (int i = 0; i < cnt; ++i) cout << '9';
      for (int i = 9; i >= 0; --i)
        if ((mask >> i) & 1) cout << i;
    }
    cout << '\n';
  }
  return 0;
}
