#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) {
  long long z = y;
  while (x % y != 0) {
    z = x % y;
    x = y;
    y = z;
  }
  return z;
}
char s[200105];
int c[11];
void fmain(int ID) {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  bool allZ = 1;
  for (int j = 2; j < n; j++)
    if (s[j] != '0') {
      allZ = 0;
      break;
    }
  if (allZ && s[1] == '1' && (s[n] == '1' || s[n] == '0')) {
    for (int(j) = 0; (j) < (int)(n - 2); (j)++) printf("9");
    puts("");
    return;
  }
  for (int(i) = 0; (i) < (int)(10); (i)++) c[i] = 0;
  for (int(i) = 1; (i) <= (int)(n); (i)++) c[s[i] - '0']++;
  for (int i = n; i; i--) {
    int num = s[i] - '0';
    c[num]--;
    for (int j = num - 1; j >= 0; j--) {
      c[j]++;
      vector<int> vp;
      for (int(z) = 0; (z) < (int)(10); (z)++)
        if (c[z] & 1) vp.push_back(z);
      if (vp.size() <= n - i) {
        int rm = n - i - vp.size();
        assert(rm % 2 == 0);
        for (int(tt) = 0; (tt) < (int)(rm); (tt)++) vp.push_back(9);
        for (int(z) = 1; (z) <= (int)(i - 1); (z)++) printf("%c", s[z]);
        printf("%d", j);
        for (int z = (int)vp.size() - 1; z >= 0; z--) printf("%d", vp[z]);
        puts("");
        return;
      }
      c[j]--;
    }
  }
}
int main() {
  int t = 1;
  scanf("%d", &t);
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
