#include <bits/stdc++.h>
using namespace std;
const double g = 10.0, eps = 1e-12;
const int N = 200000 + 10, maxn = 100000 + 10, inf = 0x3f3f3f3f,
          INF = 0x3f3f3f3f3f3f3f3f;
int num[10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    string ans = string(s.size() - 2, '9');
    for (int i = s.size() - 1; i >= 0; i--) {
      if (s[i] != '0') {
        for (char j = s[i] - 1; j >= (i == 0 ? '1' : '0'); j--) {
          memset(num, 0, sizeof num);
          string pre = s.substr(0, i) + j;
          for (int k = 0; k < pre.size(); k++) num[pre[k] - '0'] ^= 1;
          string suf;
          for (int k = 9; k >= 0; k--)
            if (num[k]) suf += (char)k + '0';
          while (pre.size() + suf.size() < s.size()) pre += '9';
          pre += suf;
          if (pre.size() == s.size()) {
            ans = pre;
            break;
          }
        }
        if (ans.size() == s.size()) break;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
