#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
char s[maxn];
int mark[10];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(mark, 0, sizeof(mark));
    int cnt = 0;
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    for (int i = 1; i <= len; ++i) mark[s[i] - '0']++;
    for (int i = 0; i <= 9; ++i) cnt += mark[i] % 2;
    int le = 0, flag = 0;
    for (int i = len; i >= 1; --i) {
      mark[s[i] - '0']--;
      if (mark[s[i] - '0'] % 2)
        cnt++;
      else
        cnt--;
      for (int j = s[i] - '0' - 1; j >= 0; --j) {
        if (j == 0 && i == 1) break;
        mark[j]++;
        if (mark[j] % 2 == 0)
          cnt--;
        else
          cnt++;
        if ((le - cnt) % 2 == 0 && le >= cnt) {
          s[i] = j + '0';
          int p = i + 1;
          while (le > cnt) s[p++] = '9', le--;
          for (int k = 9; k >= 0; --k) {
            if (mark[k] % 2) {
              s[p++] = k + '0';
            }
          }
          flag = 1;
          break;
        }
        mark[j]--;
        if (mark[j] % 2 == 0)
          cnt--;
        else
          cnt++;
      }
      if (flag) break;
      le++;
    }
    if (flag) {
      int j = 1;
      while (s[j] == '0') j++;
      for (int i = j; i <= len; ++i) {
        printf("%c", s[i]);
      }
      puts("");
    } else {
      for (int i = 1; i <= len - 2; ++i) {
        putchar('9');
      }
      puts("");
    }
  }
  return 0;
}
