#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e5 * 2 + 500;
int t, n, tmp;
char s[maxn];
int a[maxn];
int dp[10][maxn];
void slove() {
  for (int i = n; i >= 1; i--) {
    for (int j = a[i] - 1; j >= (i == 1); j--) {
      tmp = 0;
      for (int k = 0; k <= 9; k++) tmp += dp[k][i - 1] ^ (k == j);
      if (tmp <= n - i) {
        for (int k = 1; k < i; k++) printf("%d", a[k]);
        printf("%d", j);
        for (int k = 1; k <= n - i - tmp; k++) printf("9");
        for (int k = 9; k >= 0; k--)
          if (dp[k][i - 1] ^ (j == k)) printf("%d", k);
        printf("\n");
        return;
      }
    }
  }
  for (int i = 1; i <= n - 2; i++) printf("9");
  printf("\n");
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (int i = 1; i <= n; i++) {
      a[i] = int(s[i] - '0');
      for (int k = 0; k <= 9; k++) dp[k][i] = dp[k][i - 1] ^ (k == a[i]);
    }
    slove();
  }
  return 0;
}
