#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200001;
int n, cnt[MAXN][10];
char s[MAXN];
void solve() {
  for (int i = n; i; i--) {
    for (int j = s[i] - 1; j >= '0' + (i == 1); j--) {
      int res = 0;
      for (int k = 0; k < 10; k++) res += cnt[i - 1][k] ^ (k == (j - '0'));
      if (res <= n - i) {
        s[i] = j;
        for (int k = 1; k <= i; k++) printf("%c", s[k]);
        for (int k = i + 1; k <= n - res; k++) printf("9");
        for (int k = 9; k >= 0; k--)
          if (cnt[i - 1][k] ^ (k == j - '0')) printf("%c", k + '0');
        printf("\n");
        return;
      }
    }
  }
  for (int i = 1; i <= n - 2; i++) printf("9");
  printf("\n");
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (int i = 0; i < 10; i++) cnt[0][i] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 10; j++)
        cnt[i][j] = cnt[i - 1][j] ^ (j == (s[i] - '0'));
    }
    solve();
  }
}
