#include <bits/stdc++.h>
using namespace std;
int v[200100];
int sp[10];
int SP[10];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  string s;
  while (t--) {
    cin >> s;
    int n = s.size();
    for (int i = 0; i < s.size(); i++) {
      v[i + 1] = s[i] - '0';
    }
    for (int i = 0; i <= 9; i++) {
      sp[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      sp[v[i]]++;
    }
    bool OK = false;
    for (int i = n; i >= 1; i--) {
      if (OK) {
        break;
      }
      sp[v[i]]--;
      int cont = n - i;
      int last = 0;
      if (i == 1) {
        last = 1;
      }
      for (int j = v[i] - 1; j >= last; j--) {
        for (int q = 0; q <= 9; q++) {
          SP[q] = sp[q];
        }
        SP[j]++;
        int cnt = 0;
        for (int q = 0; q <= 9; q++) {
          if (SP[q] % 2) {
            cnt++;
          }
        }
        if (cnt > cont) {
          continue;
        }
        if ((cont - cnt) % 2) {
          continue;
        }
        OK = true;
        v[i] = j;
        for (int q = i + 1; q <= n; q++) {
          if (cont > cnt) {
            v[q] = 9;
            cont--;
            continue;
          }
          for (int k = 9; k >= 0; k--) {
            if (SP[k] % 2) {
              SP[k]++;
              v[q] = k;
              break;
            }
          }
        }
        break;
      }
    }
    if (OK) {
      for (int i = 1; i <= n; i++) {
        cout << v[i];
      }
      cout << '\n';
    } else {
      for (int i = 1; i <= n - 2; i++) {
        cout << 9;
      }
      cout << '\n';
    }
  }
  return 0;
}
