Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 328 ms):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
    #endif
    int tt ;
    cin >> tt ;
    while(tt--){
        string s ;
        cin >> s ;
        int n=(int)s.size() ;
        for(int u=0;u<=1000;u++){
            if(u%2){
                for(int i=n-2;i>=0;i--){
                    if((int)s[i+1]-1>(int)s[i]){
                        swap(s[i],s[i+1]) ;
                        s[i]-- ;
                    }
                }
            }
            else {
                for(int i=0;i<=n-2;i++){
                    if((int)s[i+1]-1>(int)s[i]){
                        swap(s[i],s[i+1]) ;
                        s[i]-- ;
                    }
                }   
            }
        }
        cout << s << '\n' ;
    }
    return 0;
}


Solution 2 (runtime: 109 ms):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_updat
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
#ifndef ONLINE_JUDGE
#define deb(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define deb(...)
#endif
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define forEach(arr) for (auto &x : arr)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.F << ", " << p.S << ")"; }
template <typename A>
ostream &operator<<(ostream &cout,vector<A> const &v){cout<<"[";for (int i = 0; i < v.size(); i++){if(i) cout<<", ";cout << v[i];} return cout << "]";}
template <typename A>
ostream &operator<<(ostream &cout,set<A> const &s){cout<<"[";for (auto &x: s){cout << s;} return cout << "]";}
template <typename A, typename B>
ostream &operator<<(ostream &cout,unordered_map<A, B> const &v){cout<<"{";for(auto &x: v){cout<<"{"<<x.first<<"="<<x.second<<"}";} return cout << "}";}
template <typename A>
istream &operator>>(istream &cin, vector<A> &v) {for (auto &i : v) cin >> i; return cin;}

const int mod = 1e9+7;
//=======================

void solve() {
  string s; cin>>s;
  int n = s.length();
  string ans;
  map<int, char> idx;
  for(int i=0; i<n; i++) idx[i] = s[i];
  for(int i=0; i<n; ) {
    if(s[i] == '#') {
      i++; continue;
    }
    auto it = idx.lower_bound(i+1);
    int maxValIndex = i, maxVal = s[i]-'0', covered = 1;
    while(covered <= 9 and it != idx.end()) {
      int currVal = (*it).second-'0' - covered;
      if(currVal > maxVal) {
        maxVal = currVal; 
        maxValIndex = (*it).first;
      }
      deb(i, currVal, maxVal, maxValIndex);
      it++; covered++;
    }
    s[maxValIndex] = '#';
    idx.erase(maxValIndex);
    ans += (maxVal + '0');
  }
  cout<<ans;
}


int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("error.txt", "w", stderr);
  freopen("output.out", "w", stdout);
  freopen("input.in", "r", stdin);
#endif
  auto start1 = high_resolution_clock::now();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
    deb("tc_end\n");
  }
  auto stop1 = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop1 - start1);
#ifndef ONLINE_JUDGE
  cerr << "Time: " << duration.count()<<"ms"<< endl;
#endif
  return 0;
}