Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
#define ll long long
#define vll vector<long long>
#define vi vector<int>
#define vstr vector<string>
#define str string
#define f(i, n) for (long long i = 0; i < n; i++)
#define fi(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define YES cout << "YES"<<'\n'
#define NO cout << "NO"<<'\n'
#define Yes cout << "Yes"<<'\n'
#define No cout << "No"<<'\n'
#define vpll vector<pair<ll,ll>>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define fr2(i,a,b) for(ll i=a;i>=b;i--)
#define F first
#define S second
int mod = 1e9+7;
using namespace std;

void bankai()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
ll gcd(ll a, ll b)
{
    if (b == 0)return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b) * b);
}
bool prime(ll n)
{
    if (n <= 1)return false;
    if (n <= 3)return true;
    if (n % 2 == 0)return false;
    for (ll i = 3; i * i <= n; i += 2)
    {
        if (n % i == 0)return false;
    }
    return true;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second > b.second);
}
bool is_palindrome(str s)
{
    ll mid = s.size()/2;
    ll n = s.size();
    f(i, mid)
    {
        if(s[i] != s[n - i - 1])return false;
    }
    return true;
}
long long calculateFactorial(ll n)
{
    if (n == 0 || n == 1)
    {
        return 1;
    }
    return n * calculateFactorial(n - 1);
}
ll Digit_Sum(int a)
{
    int sum = 0;
    for (int i = a; i > 0; i /= 10)
    {
        sum += i % 10;
    }
    return sum;
}
int nth_sqr_func(ll mid, ll n)
{
    ll ans = 1;
    while(n>0)
    {
        ans=ans*mid;
        n--;
    }
    return ans;
}
set<ll>s;
int NOD(int n)
{
    for(ll i = 1 ; i <= sqrt(n) ; i++)
    {
        if(n%i==0)
        {
            ll x = i;
            ll y = n/i;
            s.insert(x);
            s.insert(y);
        }
    }
}
ll ans = 0;
void prime_fact(ll n)
{
    ans = 0;
    for(ll i = 2 ; i*i <= n ; i++)
    {
        if(n%i==0)
        {
            ans++;
            //ll cnt = 0;
            while(n%i==0)
            {
                //cnt++;
                n/=i;
            }

            //cout << i << " ";//<< "^" << cnt << " ";
        }

    }
    if(n>1)ans++;
    cout << ans << '\n';
    // else cout << '\n';
}


//-------------------------------------------------//
//               main code here                    //
//-------------------------------------------------//


void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    bool f = true;

    while (f)
    {
        f = false;

        for (int i = 1; i < n; i++)
        {
            if (s[i] > '0')
            {
                str temp = s;
                temp[i]--;
                swap(temp[i], temp[i - 1]);

                if (temp > s)
                {
                    s = temp;
                    f = true;
                }
            }
        }
    }

    cout << s << '\n';
}




int main()
{
    bankai();

    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
//4 7
//1 2 5 9

//6
//  92 82 21 16 18 95




Submission (verdict: Time limit exceeded on test 8):
#include <bits/stdc++.h>
#define ll long long
#define vll vector<long long>
#define vi vector<int>
#define vstr vector<string>
#define str string
#define f(i, n) for (long long i = 0; i < n; i++)
#define fi(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define YES cout << "YES"<<'\n'
#define NO cout << "NO"<<'\n'
#define Yes cout << "Yes"<<'\n'
#define No cout << "No"<<'\n'
#define vpll vector<pair<ll,ll>>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define fr2(i,a,b) for(ll i=a;i>=b;i--)
#define F first
#define S second
int mod = 1e9+7;
using namespace std;

void bankai()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
ll gcd(ll a, ll b)
{
    if (b == 0)return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b) * b);
}
bool prime(ll n)
{
    if (n <= 1)return false;
    if (n <= 3)return true;
    if (n % 2 == 0)return false;
    for (ll i = 3; i * i <= n; i += 2)
    {
        if (n % i == 0)return false;
    }
    return true;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second > b.second);
}
bool is_palindrome(str s)
{
    ll mid = s.size()/2;
    ll n = s.size();
    f(i, mid)
    {
        if(s[i] != s[n - i - 1])return false;
    }
    return true;
}
long long calculateFactorial(ll n)
{
    if (n == 0 || n == 1)
    {
        return 1;
    }
    return n * calculateFactorial(n - 1);
}
ll Digit_Sum(int a)
{
    int sum = 0;
    for (int i = a; i > 0; i /= 10)
    {
        sum += i % 10;
    }
    return sum;
}
int nth_sqr_func(ll mid, ll n)
{
    ll ans = 1;
    while(n>0)
    {
        ans=ans*mid;
        n--;
    }
    return ans;
}
set<ll>s;
int NOD(int n)
{
    for(ll i = 1 ; i <= sqrt(n) ; i++)
    {
        if(n%i==0)
        {
            ll x = i;
            ll y = n/i;
            s.insert(x);
            s.insert(y);
        }
    }
}
ll ans = 0;
void prime_fact(ll n)
{
    ans = 0;
    for(ll i = 2 ; i*i <= n ; i++)
    {
        if(n%i==0)
        {
            ans++;
            //ll cnt = 0;
            while(n%i==0)
            {
                //cnt++;
                n/=i;
            }

            //cout << i << " ";//<< "^" << cnt << " ";
        }

    }
    if(n>1)ans++;
    cout << ans << '\n';
    // else cout << '\n';
}


//-------------------------------------------------//
//               main code here                    //
//-------------------------------------------------//


void solve()
{
    str s;
    cin >> s;
    int n = s.size();
    bool f = true;

    while (f)
    {
        f = false;

        for (int i = n - 1; i > 0; --i)
        {
            if (s[i] > '0')
            {
                char temp = s[i];
                s[i]--;
                if (s[i] > s[i-1])
                {
                    swap(s[i], s[i-1]);
                    f = true;
                }
                else
                {
                    s[i] = temp;
                }
            }
        }
    }

    cout << s << '\n';
}




int main()
{
    bankai();

    int t;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
//4 7
//1 2 5 9

//6
//  92 82 21 16 18 95