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

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

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pllll = pair<long long, long long>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vpii = vector<pair<int, int>>;
using vpllll = vector<pair<long long, long long>>;
using mpii = map<int, int>;
using mpci = map<char, int>;
using mpcl = map<char, ll>;
using mpsi = map<string, int>;
using mpsl = map<string, ll>;
using mpll = map<ll, ll>;
using mpii = map<int, int>;
using mpipii = map<int, pair<int, int>>;
using mpllpllll = map<long long, pair<long long, long long>>;
#define ilen(a) (int)a.size()
#define llen(a) (ll) a.size()
#define all(x) (x).begin(), (x).end()
#define fi(i, n) for (int i = 0; i < n; i++)
#define fl(i, n) for (ll i = 0; i < n; i++)
#define fla(i, a, b) for (ll i = (a); i <= (b); i++)
const char nl = '\n';
const int intmax = INT_MAX;
const int intmin = INT_MIN;
const ll llmax = LLONG_MAX;
const ll llmin = LLONG_MIN;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    for (int i = 1; i < n; i++)
    {
        if (!(s[i] > s[i - 1] + 1))
        {
            continue;
        }
        char a = s[i];
        int count = 1;
        int j = i - 1;
        while (j >= 0 && s[j] < a - count)
        {
            count++;
            j--;
        }
        if (j < 0)
        {
            j = -1;
        }

        s.erase(s.begin() + i);
        s.insert(s.begin() + j + 1, (char)(a - count + 1));
    }

    cout << s << endl;
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int INP;
    cin >> INP;
    while (INP--)
    {
        solve();
    }
    return 0;
}

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



using namespace std;

#define all(x) (x).begin(), (x).end()

#define allr(x) (x).rbegin(), (x).rend()

typedef long long ll;

typedef long double ld;

const ll mod = 1e9 + 7;

const ll MOD = 998244353;



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    ll tt;

    cin >> tt;

    while (tt--) {

        string st;

        cin >> st;

        vector<ll> s(st.size());

        

        // Convert string to vector of digits

        for (ll i = 0; i < s.size(); i++) {

            s[i] = st[i] - '0';

        }



        // Iterate over each element of the array and apply the transformation rules

        for (ll i = 1; i < s.size(); i++) {

            // We need to make sure we are within valid bounds for j

            for (ll j = i - 1; j >= max(0ll, i - 9ll); j--) {

                // Ensure the current swap is valid by checking bounds

                if (s[j+1] > s[j] + 1) {

                    ll temp = s[j];

                    s[j] = s[j+1] - 1;

                    s[j+1] = temp;

                }

                else {

                    break; 

                }

            }

        }



        string ans="";

        for (ll i = 0; i < s.size(); i++) {

            ans+=s[i]+'0';

        }

        

        cout << ans << endl;

    }



    return 0;

}