#include <bits/stdc++.h>
using namespace std;
char s[200007];
int a[11], T;
bool ck(int x) {
  int sum = 0;
  for (int i = 0; i < 10; ++i) sum += a[i];
  if (sum <= x && (x - sum) % 2 == 0) return 1;
  return 0;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    memset(a, 0, sizeof(a));
    int f = 0;
    scanf(" %s", s);
    int len = strlen(s);
    for (int i = 0; i < len; ++i) a[s[i] -= '0'] ^= 1;
    for (int i = len - 1; i >= 0; --i) {
      a[s[i]] ^= 1;
      for (int j = s[i] - 1; j >= (i == 0 ? 1 : 0); --j) {
        a[j] ^= 1;
        if (ck(len - i - 1)) {
          for (int k = 0; k < i; ++k) printf("%d", s[k]);
          printf("%d", j);
          int sum = 0;
          for (int k = 0; k < 10; ++k) sum += a[k];
          for (int k = 1; k <= len - i - 1 - sum; ++k) printf("9");
          for (int k = 9; k >= 0; --k)
            if (a[k]) printf("%d", k);
          printf("\n");
          f = 1;
          break;
        }
        a[j] ^= 1;
      }
      if (f) break;
    }
    if (!f) {
      for (int i = 0; i < len / 2 * 2 - 2; ++i) printf("9");
      printf("\n");
    }
  }
  return 0;
}
