Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Time limit exceeded on test 8):
// #include <cmath>

#include <bits/stdc++.h> 

using namespace std;

// ================================TYPEDEFs======================================================

#define int          long long

#define vi           vector<int>

#define vf           vector<float>

#define vd           vector<double>

#define vc           vector<char>

#define vs           vector<string>

#define vvi          vector<vector <int>>

#define vvc          vector<vector<char>>

#define vb           vector<bool>

#define si           set<int>

#define msi          multiset<int>

#define debug        cout<<"working here..."<<endl;

#define rv(i,vt)     for (auto i:vt)

#define FOR(i,n)     for( int i=0; i < n; i++) 

#define FORR(i,p,n)  for(int i=p; i < n; i++)

#define FORB(i,m,n)  for (int i=m;i>n;i--)

#define vllin(vec,n) FOR(i, n){int k; cin >>k ; vec.pb(k);}

#define viin(vec,n)  FOR(i, n){int k; cin >>k ; vec.pb(k);}

#define arrin(arr,n) FOR(i, n){int k; cin >>arr[i];}

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

#define uniq(arr)    {sort(all(arr));arr.erase(unique(all(arr)),arr.end());}

#define pb           push_back

#define pob          pop_back

#define lb           lower_bound

#define ub           upper_bound

#define mp           make_pair

#define PI           3.1415926535897932384626433832795

#define MOD1          1000000007

#define MOD         998244353

#define NMAX         1000007

#define MP           make_pair

#define f            first 

#define s            second

#define cntbit(x)    __builtin_popcountll(x)

#define ce(i)         cout<<i<<endl

#define pnl          cout<<endl



// ================================FUNCTIONS======================================================

void y_n(bool check){

    if(check){

        cout << "YES" << endl;

    }

    else{

        cout << "NO" << endl;

    }

    return;

    }

int gcd(int a, int b)

{

    return b == 0 ? a : gcd(b, a % b);   

}



// int lcm(int a, int b){

//     return (a*b)/gcd(a,b);

// }



bool float_compare(double a, double b) {

    double tolerance = 1e-9;

    return std::abs(a - b) <= tolerance;

}



// template<class elemType>

void pv(vi vec){

    int n=vec.size();

    FOR(i,n){

        cout << vec[i] << " \n"[i==n-1];

    }

    }

// template<class elemType>

// void max_vector(elemType * vec){

//     return *max_element(vec.begin(), vec.end());}

template<class elemType>

elemType max_vector(const std::vector<elemType>& vec) {

    return *std::max_element(vec.begin(), vec.end());

}



void print_array( int* arr, int size) {

    for (int i = 0; i < size; i++) {

        cout << arr[i] << " ";

    }

    cout << endl;

    }







vi primeFactorization(int n){

    vi factors;

    while (n % 2 == 0)

    {

        factors.pb(2);

        n = n/2;

    }

    for (int i = 3; i <= sqrt(n); i = i + 2)

    {

        while (n % i == 0)

        {

            factors.pb(i);

            n = n/i;

        }

    }

    if (n > 2) factors.pb(n);

    return factors;

    }



int sumofarray( vi arr){

    int sum=0;

    int size=arr.size();

    FOR(i,size){

        sum+=arr[i];

    } 

    return sum;

    }



string trim(const string& str) {

    // Find the first non-space character

    auto start = find_if_not(str.begin(), str.end(), ::isspace);

    

    // If the string is empty or contains only spaces

    if (start == str.end()) {

        return "";

    }

    

    // Find the last non-space character

    auto end = find_if_not(str.rbegin(), str.rend(), ::isspace).base();

    

    // Return the substring from the first non-space to the last non-space

    return string(start, end);

}





vi prefix_sum(vi vec){

    

    vi prefx_sum;

    int sum = 0;

    FOR(i,vec.size()){

        sum+=vec[i];

        prefx_sum.pb(sum);

    }

    return prefx_sum; 

    }



vi suffix_sum(vi vec){

    vi suffx_sum;

    int sum = 0;

    FOR(i,vec.size()){

        sum+=vec[vec.size()-i-1];

        suffx_sum.pb(sum);

    }

    reverse(all(suffx_sum));

    return suffx_sum; 

    }



void reverseArray(int arr[], int start, int end){

    while (start < end)

    {

        int temp = arr[start]; 

        arr[start] = arr[end];

        arr[end] = temp;

        start++;

        end--;

    }

    }

bool isPrime(int n){

    if (n <= 1)

        return false;

    for (int i = 2; i * i <= n; i++) 

    {

        if (n % i == 0)

            return false;

    }

    return true;

    }



int count_x(int number,int x){

    int count = 0;

    while(true){

        if ((number%x)==0){

            count++;

            number/=x;

        }

        else{

            return count;

        }

    }

    return 0;

}





// double myPow(double x, int n) {

//         if (x==1) return 1;

//         if (n<0) return 1/(myPow(x,abs(n)));

//         if (n==1) return x;

//         else if (n==0) return 1;

//         else if (n%2==0) return myPow(x*x,n/2);

//         else return (x*(myPow((x*x),(n-1)/2)));

//         return x;

//     }



int accurateCeil(int a, int b) {

    int val = a / b;

    while (val * b < a)

        val++;

    return val;

}

int nCr(int n, int r) {

    if (r > n) return 0;

    if (r == 0 || n == r) return 1;

    double res = 0;

    for (int i = 0; i < r; i++) {

        res += log(n-i) - log(i+1);

    }

    return round(exp(res));

}

long long binomialCoefficientMod(int n, int r) {

    if (r > n - r) {

        r = n - r;  // Choosing the smaller of r and (n-r) for optimization

    }



    vi dp(r + 1, 0);

    dp[0] = 1;



    for (int i = 1; i <= n; ++i) {

        for (int j = min(i, r); j > 0; --j) {

            dp[j] = (dp[j] + dp[j - 1]) % MOD;  // Apply modulo operation

        }

    }



    return dp[r];

}







vvi generateSubsets(int n) {

    vvi ans;

    for (int mask = 0; mask < (1 << n); mask++) {

        vector<int> subset;

        for (int i = 0; i < n; i++) {

            if (mask & (1 << i)) {

                subset.push_back(i + 1); // Assuming the set elements are 1-indexed

            }

        }

        // Process the subset (in this example, just printing it)

        ans.pb(subset);

    }

    return ans;

}





vi maxSubArraySum(vi &a)

{   int size=a.size();

    int max_so_far = INT_MIN, max_ending_here = 0,

        start = 0, end = 0, s = 0;



    for (int i = 0; i < size; i++) {

        max_ending_here += a[i];



        if (max_so_far < max_ending_here) {

            max_so_far = max_ending_here;

            start = s;

            end = i;

        }



        if (max_ending_here < 0) {

            max_ending_here = 0;

            s = i + 1;

        }

    }

    return {max_so_far,start,end};

    // cout << "Maximum contiguous sum is " << max_so_far

    //      << endl;

    // cout << "Starting index " << start << endl

    //      << "Ending index " << end << endl;

}





vi divisors(int n){

    vi ans;

    FORR(i,2,ceil(sqrt(n)+1)){

        if((n%i)==0) {

            ans.pb(i);

            if(i*i!=n) ans.pb(n/i);

            }

    }

    uniq(ans);

    return ans;

}



// int num_intrsc(int l1,int r1,int l2,int r2){

//     // return max(0ll,min(r1,r2)-l2+1);

//     return max(0ll,min(r1,r2)-max(l1,l2)+1);

// }





string bin(int num){

    bitset<64> bits(num);

    return bits.to_string();

    // return ans;

}















// ==========================FACTORIZATION USING SIEVE=========================



#define MAXN 1



// stores smallest prime factor for every number

int spf[MAXN];



// Calculating SPF (Smallest Prime Factor) for every

// number till MAXN.

// Time Complexity : O(nloglogn)

void sieve()

{

    spf[1] = 1;

    for (int i = 2; i < MAXN; i++)



        // marking smallest prime factor for every

        // number to be itself.

        spf[i] = i;



    // separately marking spf for every even

    // number as 2

    for (int i = 4; i < MAXN; i += 2)

        spf[i] = 2;



    for (int i = 3; i * i < MAXN; i++) {

        // checking if i is prime

        if (spf[i] == i) {

            // marking SPF for all numbers divisible by i

            for (int j = i * i; j < MAXN; j += i)



                // marking spf[j] if it is not

                // previously marked

                if (spf[j] == j)

                    spf[j] = i;

        }

    }

}



// A O(log n) function returning primefactorization

// by dividing by smallest prime factor at every step

vi getFactorization(int x)

{

    vector<int> ret;

    while (x != 1) {

        ret.pb(spf[x]);

        x = x / spf[x];

    }

    return ret;

}





template<typename T> struct iterseg{

    // starts with iterseg<int> sgtr;

    const T ID =-1; // initial value of elements

    T comb(T a,T b){ //type of segment tree

        return max(a,b);

    }

    int n;vector<T> tree;

    void init(int _n){  //initializes with size n

        n=_n;

        tree.assign(2*n,ID);

    }

    void pull(int p){

        tree[p]=comb(tree[2*p],tree[2*p+1]);

    }

    void upd(int p,T val){ // updates at index p with val

        p+=n;

        tree[p]=val;

        for (p/=2;p;p/=2){

            pull(p);

        }

    }

    T query(int l,int r){ // answer query for range

        l+=n; r+=n+1;

        T ra=ID;

        T rb=ID;

        for (l,r;l<r;l/=2,r/=2){

            if (l&1) ra=comb(ra,tree[l++]);

            if (r&1) rb=comb(tree[--r],rb);

        }

        return comb(ra,rb);

    }

};



int mex(vector<int> &arr1)

{   

    int N=arr1.size();

    vi arr=arr1;

    sort(arr.begin(), arr.end());



    int mex = 0;

    for (int idx = 0; idx < N; idx++) if (arr[idx] == mex) mex += 1;

    return mex;

}



bool isPowerOfTwo(int n) {

    return (n > 0) && ((n & (n - 1)) == 0);

}

int binpow(int x, int y, int p) {

    int res = 1; 

    x = x % p; 

    while (y > 0) {

        if (y & 1) res = (res * x) % p;

        y/=2;

        x = (x * x) % p; // Change x to x^2

    }

    return res;

}





int modInverse(int a, int p) {

    return binpow(a, p - 2, p);

}

// ============================================================================================



// ===================================CODE GOES HERE===========================================



int solve(){

    string s;

    cin>>s;

    vi a;

    int n=(int)s.size();

    FOR(i,n) a.pb(s[i]-'0');

    // pv(a);

    // ce("here");

    vi ans;

    vi used(n,0);

    int i=0;

    while(i<n){

        // ce(i);

        if(used[i]) {i++;continue;}

        int j=i+1;

        int pos=i;

        int val=a[i];

        int counter=1;

        while(j<n){

            if(counter==10) break;

            if(used[j]) {j++;continue;}

            if(a[j]-counter>val){

                val=a[j]-counter;

                pos=j;

            }

            counter++;

            j++;

        }

        ans.pb(val);

        used[pos]=true;

        if(pos==i){

            i++;

            // ans.pb(a[i]);

        }

        // pv(ans);

    }



    FOR(i,n) cout<<(char)('0'+ans[i]);

    ce("");

    // pv(ans);

    return 0;

}



signed main() {

    ios::sync_with_stdio(0);cin.tie(0);

    int tests = 1;

    cin>>tests;

    // sieve();  

    FOR(_,tests) {solve();}

    return 0;

}