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

Solution 1 (runtime: 577 ms):
#include "bits/stdc++.h"
using namespace std;

#define lli            long long int
#define ll             long long
// #define gcd            __gcd
#define setbits(x)     __builtin_popcountll(x)
#define zrobits(x)     __builtin_ctzll(x)
#define mod            1000000007
#define mod2           998244353
#define maxe           *max_element
#define mine           *min_element
#define inf            1e18
#define pb             push_back
#define all(x)         x.begin(), x.end()
#define lb             lower_bound
#define ub             upper_bound
#define ins            insert
#define sz(x)          (int)(x).size()
#define mk             make_pair
#define deci(x, y)     fixed<<setprecision(y)<<x
#define w(t)           int t; cin>>t; while(t--)
#define hemant         ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PI             3.141592653589793238
#define mem0(x)        memset(x,0,sizeof x)
#define mem1(x)        memset(x,-1,sizeof x)
#define nl              "\n"
#define yy             cout<<"YES"<<"\n"
#define nn             cout<<"NO"<<"\n"
#define vi             vector<int> 
#define vll            vector<long long>
#define vvi            vector<vector<int> >
#define ff             first
#define ss             second
#define debug(x)       cerr << #x <<" "; _print(x); cerr << endl;

typedef unsigned long long ull;
typedef long double lld;

ll modular_expo(long long x, unsigned ll y, int p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
vector<ll> sieve(ll n) {ll *arr = new ll[n + 1](); vector<ll> vect; for (ll i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (ll j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll primefactorscount(ll n){vector<ll> v1=sieve(n);ll cnt=0;for(ll i=0;i<sz(v1);i++){if(n%v1[i]==0){cnt++;}}return cnt;}
int mod_add(int a, int b) {a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
 
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(deque <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(list <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T, class V> void _print(multimap <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(deque <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(list <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

bool isPower(ll x, ll y)
{
    long double res1 = log(y) / log(x);
    return res1==floor(res1);
}

void solve(){
    w(t){
        string s;
        cin>>s;
        int n=s.length();
        if (n==1){
            cout<<s<<nl;
            continue;
        }
        map<int, pair<int, int> > mp;
        for(int i=0;i<n-1;i++){
            // cout<<s[i]<< " ";
            int a1 = (int)s[i] - '0';
            int a2 = (int)s[i+1] - '0';
            mp[i] = make_pair(a1, a2);
        }
        // debug(mp)
        int i=0;
        int exists = 0;
        while(true){
            if (i == n-1){
                if (exists == 1){
                    i=0;
                    exists = 0;
                    continue;
                }else{
                    break;
                }
            }
            if ( i == 0){
                pair<int, int> p1 = mp[i];
                if (p1.first + 1 < p1.second){
                    int x = p1.first;
                    int y = p1.second;
                    p1.first = y - 1;
                    p1.second = x;
                    if (i + 1 < n){
                        pair<int, int> p2 = mp[i + 1];
                        p2.first = x;
                        mp[i + 1] = p2;
                        mp[i] = p1;
                        exists =1;
                    }
                }
            }
            else if( i == n-2 ){
                pair<int, int> p1 = mp[i];
                if (p1.first + 1 < p1.second){
                    int x = p1.first;
                    int y = p1.second;
                    p1.first = y - 1;
                    p1.second = x;
                    if (i - 1 >= 0){
                        pair<int, int> p2 = mp[i - 1];
                        p2.second = y-1;
                        mp[i-1] = p2;
                        mp[i] = p1;
                        exists = 1;
                    }
                }
            }
            else{
                pair<int, int> p1 = mp[i];
                if (p1.first + 1 < p1.second){
                    int x = p1.first;
                    int y = p1.second;
                    p1.first = y - 1;
                    p1.second = x;
                    if (i + 1 < n and i - 1 >=0){
                        pair<int, int> p2 = mp[i + 1];
                        pair<int, int> p3 = mp[i - 1];
                        p2.first = x;
                        p3.second = y-1;
                        mp[i-1] = p3;
                        mp[i + 1] = p2;
                        mp[i] = p1;
                        exists = 1;
                    }
                }
            }
            // debug(mp)
            i++;
        }
        // debug(mp)
        for(int i = 0; i < n; i++) {
            if(i == n - 1) {
                pair<int, int> p = mp[i - 1]; // Use i - 1 to get the last pair
                char ch1 = p.second + '0';
                cout << ch1;
            } else {
                char ch1 = mp[i].first + '0';
                cout << ch1;
            }
        }
        cout<<nl;
    }
}
int32_t main(){
    hemant;
    solve();
}

Solution 2 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;

void solve(string s) {

    int n = s.size();

    for(int i = 0; i < n; i++) {
        //check for i + 9 digits
        int k = i;
        int maxi = s[i] - '0';

        //check 9 elements forward from i and place the max possible on i'th index
        for(int j = i + 1; j < min(i + 10, n); j++) {
            int diff = s[j] - '0' - (j - i);
            if(diff > maxi) {
                k = j;
                maxi = diff;
            }
        }
        //keep swapping until k'th element is at i
        while(k > i) {
            s[k]--;
            swap(s[k], s[k - 1]);
            k--;
        }
    }

    cout << s << endl;
}

int main() {
    int tests;
    cin >> tests;
    vector<string> nums;

    for(int i = 0; i < tests; i++) {
        string s;
        cin >> s;
        nums.push_back(s);
    }

    for(int i = 0; i < tests; i++) {
        solve(nums[i]);
    }
    return 0;
}


void bruteForce(string s) {

    while(true) {
        int maxi = 1;
        int k = -1;
        //go through the string, and check for the max difference
        for(int i = 1; i < s.size(); i++) {
           int d = s[i] - s[i - 1];

            if(d > maxi) {
                k = i;
                maxi = d;
            }
            
        }
        if(k == -1)
            break;
        //decrement k'th digit by 1, swap with the left
        s[k]--;
        swap(s[k], s[k - 1]);
    }
    cout << s << endl;
}