#include<bits/stdc++.h>
using namespace std;
#define int long long

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

    for(int i = 0; i<n; i++)
    {
        int d = s[i] - '0';
        int mx = 0;
        int mx_indx = 0;
        for(int j = i+1; j<min(i + 9 - d, n); j++)
        {
            // cout << "I : " << i << "   J : " << j << endl;
            // cout << "S[i] : " << s[i] << "   S[j] : " << s[j] << endl;
            // cout << s[j] - i - '0' << endl;
            if(s[j] - i > s[i] && s[j] - i - '0' > mx)
            {
                mx = s[j] - i - '0';
                mx_indx = j;
            }
        }
        cout << "MX INDX : " << mx_indx << endl;
        cout << s << endl;

        if(mx)
        {
            while(mx_indx != i)
            {
                s[mx_indx] = s[mx_indx] - 1;
                swap(s[mx_indx], s[mx_indx-1]);
                mx_indx--;
            }
        }
    }

    cout << s << endl;

}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--)
        solve();
}

#include<bits/stdc++.h>
using namespace std;
#define int long long

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

    for(int i = 0; i<n; i++)
    {
        int mx = s[i] - '0';
        int mx_indx = i;
        for(int j = i+1; j<min(i + 10, n); j++)
        {
            if(s[j] - (j - i) - '0' > mx)
            {
                mx = s[j] - (j - i) - '0';
                mx_indx = j;
            }
        }

        if(mx)
        {
            while(mx_indx > i)
            {
                swap(s[mx_indx], s[mx_indx-1]);
                mx_indx--;
            }
        }
        s[i] = char(mx + '0');
    }

    cout << s << endl;

}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--)
        solve();
}

Answer: 2


#include <iostream>
#include <queue>
using namespace std;

string solve(string s) {
    for(int i = 0; i < s.length() - 1; ++i) {
        int max = -1;
        int max_index = -1;
        for(int j = i; j <= i + 9 && j < s.length(); ++j) {
            int d = s[j] - '0';
            if(d - (j - i) > max) {
                max = d;
                max_index = j;
            }
        }

        s[max_index] = (char) (s[max_index] - '0' - (max_index - i) + '0');
        for(int j = max_index; j > i; --j) {
            char temp = s[j - 1];
            s[j - 1] = s[j];
            s[j] = temp;
        }
    }
    return s;
}

int main() {
    int t;
    cin >> t;
    for(int i = 0; i < t; ++i) {
        string s;
        cin >> s;
        cout << solve(s);
    }
}

#include <iostream>
#include <queue>
using namespace std;

string solve(string s) {
    for(int i = 0; i < s.length() - 1; ++i) {
        int max = -1;
        int max_index = -1;
        for(int j = i; j <= i + 9 && j < s.length(); ++j) {
            int d = s[j] - '0';
            if(d - (j - i) > max) {
                max = d - (j - i);
                max_index = j;
            }
        }

        s[max_index] = (char) (s[max_index] - '0' - (max_index - i) + '0');
        for(int j = max_index; j > i; --j) {
            char temp = s[j - 1];
            s[j - 1] = s[j];
            s[j] = temp;
        }
    }
    return s;
}

int main() {
    int t;
    cin >> t;
    for(int i = 0; i < t; ++i) {
        string s;
        cin >> s;
        cout << solve(s) << "\n";
    }
}

Answer: 2


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using orderStaticTree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long

#define saleh                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);

const int md = 1e9 + 7;

int main()
{
    saleh;
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int n = s.size();
        for (int i = n - 1; i >= 1; i--)
        {
            int curr = s[i] - '0';
            --curr;
            int pre = s[i - 1] - '0';
            if (curr > pre)
            {

                s[i - 1] = char(curr + '0');
                s[i] = char(pre + '0');
            }
            int j = i;
            int ii = i + 1;
            while (ii <= n - 1)
            {
                int curr = s[j] - '0';
                int nxt = s[ii] - '0';
                nxt--;
                if (nxt > curr)
                {
                    s[j] = char(nxt + '0');
                    s[ii] = char(curr + '0');
                }
                j++;
                ii++;
            }
        }
        cout << s << endl;
    }

    return 0;
}

#include <iostream>

#include <vector>

#include <algorithm>

#include <queue>

#include <map>

#include <set>

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

template <class T>

using orderStaticTree =

    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long

#define saleh                         \

    ios_base::sync_with_stdio(false); \

    cin.tie(nullptr);

const int md = 1e9 + 7;

int main()

{

    saleh;

    int t;

    cin >> t;

    while (t--)

    {

        string s;

        cin >> s;

        int n = s.size();

        for (int i = n - 1; i >= 1; i--)

        {

            int curr = s[i] - '0';

            --curr;

            int pre = s[i - 1] - '0';

            if (curr > pre)

            {

                s[i - 1] = char(curr + '0');

                s[i] = char(pre + '0');

            }

            int j = i;

            int ii = i + 1;

            while (ii <= n - 1 && s[ii] != '0' && s[j] < s[ii])

            {

                int curr = s[j] - '0';

                int nxt = s[ii] - '0';

                nxt--;

                if (nxt > curr)

                {

                    s[j] = char(nxt + '0');

                    s[ii] = char(curr + '0');

                }

                j++;

                ii++;

            }

        }

        cout << s << endl;

    }

    return 0;

}

Answer: 2


def recursive(a,begin):
    if begin==len(a):
        return
    max_val = a[begin]
    max_index = begin
    for i in range(begin, min(len(a), begin+8)):
        if max_val<(a[i] - (i-begin)):
            max_val= a[i]-i+begin
            max_index = i
    while max_index !=begin:
        a[max_index], a[max_index-1] = a[max_index-1],a[max_index]-1
        max_index-=1
def solve():
    n = input()
    a = list(map(int,n))
    begin = 0
    while begin<len(a):
        recursive(a,begin)
        begin+=1
    for i in a:
        print(i,end='')
    print('\n')
x = int(input())
for i in range(x):
    solve()

def recursive(a,begin):
    if begin==len(a):
        return
    max_val = a[begin]
    max_index = begin
    for i in range(begin, min(len(a), begin+9)):
        if max_val<(a[i] - (i-begin)):
            max_val= a[i]-i+begin
            max_index = i
    while max_index !=begin:
        a[max_index], a[max_index-1] = a[max_index-1],a[max_index]-1
        max_index-=1
def solve():
    n = input()
    a = list(map(int,n))
    begin = 0
    while begin<len(a):
        recursive(a,begin)
        begin+=1
    print(''.join(map(str, a)))
x = int(input())
for i in range(x):
    solve()

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define int long long int

void solve(){
	int n;

	string a;
	cin>> a;
	n = a.size();

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

		int mx = a[i]-'0', mxi=i;

		for (int j = i+1; j < i+9 && j < n; ++j)
		{
			if(mx < (a[j]-'0'-(j-i))){
				mx = (a[j]-'0'-(j-i));

				mxi = j;
			}
		}

		// swap

		int c = a[i], cn = 0;
		for (int k = i+1; k <= mxi; ++k)
		{
			cn = a[k];
			a[k] = c;
			c = cn;
		}
		a[i] = mx+'0';

		// cerr<< a <<",";
	}

	// cerr<<endl;

	cout<< a << endl;
}

int32_t main(){

	std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); std::cout.tie(nullptr);
	// string s;
	// getline(cin, s);

	// int i = 0;
	// while(s[i] != '\0'){
	// 	cout<< s[i];
	// 	i++;
	// }

	// cout<< s << endl;

	int t = 0;

    cin>> t;
    while(t--){
    	solve();
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long int;

void solve(){
	int n;

	string a;
	cin>> a;
	n = a.size();

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

		int mx = a[i]-'0', mxi=i;

		for (int j = i+1; j < n; ++j)
		{
			if(mx < (a[j]-'0'-(j-i))){
				mx = (a[j]-'0'-(j-i));

				mxi = j;
			}
		}

		// swap

		int c = a[i], cn = 0;
		for (int k = i+1; k <= mxi; ++k)
		{
			cn = a[k];
			a[k] = c;
			c = cn;
		}
		a[i] = mx+'0';

		// cerr<< a <<",";
	}

	// cerr<<endl;

	cout<< a << endl;
}

int main(){
	// string s;
	// getline(cin, s);

	// int i = 0;
	// while(s[i] != '\0'){
	// 	cout<< s[i];
	// 	i++;
	// }

	// cout<< s << endl;

	int t = 0;

    cin>> t;
    while(t--){
    	solve();
    }

    return 0;
}

Answer: 1


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

    int t;

    cin>>t;

    while(t--){

  string s;

  cin>>s;

  ll n=s.size();

  ll i;

  vector<ll>v(n);

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

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

  }

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

      ll  m=LLONG_MIN;

      ll idx=-1;

      ll k=1;

      for(ll j=i+1; j<i+9; j++){

          if(v[j]-k > m){

              idx=j;

              m=v[j]-k;

          }

          k++;

      }

      if(m>v[i]){

          for(int y=idx; y>i; y--){

            v[y]=v[y-1];

          }

          v[i]=m;

      }

  }

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

      ll m=LLONG_MIN;

      ll idx=-1;

      ll k=1;

      for(ll j=l+1; j<n; j++){

          if(v[j]-k > m){

              idx=j;

              m=v[j]-k;

          }

          k++;

      }

     // cout<<m<<endl;

      if(m>v[l]){

          for(ll y=idx; y>l; y--){

            v[y]=v[y-1];

          }

          v[l]=m;

      }

//       for(auto it:v){

//       cout<<it;

//   }

//   cout<<endl;

  }

  for(auto it:v){

      cout<<it;

  }

  cout<<endl;

    }

}

#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;

    cin>>t;

    while(t--){

  string s;

  cin>>s;

  int n=s.size();

  int i;

  vector<int>v(n);

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

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

  }

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

      int m=INT_MIN;

      int idx=-1;

      int k=1;

      for(int j=i+1; j<i+9; j++){

          if(v[j]-k > m){

              idx=j;

              m=v[j]-k;

          }

          k++;

      }

      if(m>v[i]){

          int z=1;

          for(int k=idx; k>i; k--){

              v[k]=v[k]-z;

              swap(v[k],v[k-1]);

              z++;

          }

      }

  }

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

      int m=INT_MIN;

      int idx=-1;

      int k=1;

      for(int j=l+1; j<n; j++){

          if(v[j]-k > m){

              idx=j;

              m=v[j]-k;

          }

          k++;

      }

     // cout<<m<<endl;

      if(m>v[l]){

          for(int y=idx; y>l; y--){

            v[y]=v[y-1];

          }

          v[l]=m;

      }

//       for(auto it:v){

//       cout<<it;

//   }

//   cout<<endl;

  }

  for(auto it:v){

      cout<<it;

  }

  cout<<endl;

    }

}

Answer: 1


#include <iostream>
#include <string>
using namespace std;
using ll = long long;

int main() {
    int t;
    cin >> t;

    while (t--) {

        string s;
        cin >> s;
        ll n = s.size();
        for (ll i = 0; i < n; i++) {
            ll maxc = s[i] - '0';
            ll maxi = i;
            for (ll j = i ; j < min(i+10,n); j++) {
                if (s[j] - '0' + (i - j) > maxc) {
                    maxc = s[j] - '0' + (i - j);
                    maxi = j;
                }
            }ll k = maxi;

            while(maxi > i) {
                swap(s[maxi], s[maxi - 1]);
                maxi--;
            }
            s[i] += (i - k);
        }

        cout <<s << endl;
    }

    return 0;
}

#include <iostream>
#include <string>
using namespace std;
using ll = long long;

int main() {
    int t;
    cin >> t;

    while (t--) {

        string s;
        cin >> s;
        ll n = s.size();
        for (int i = 0; i < n; i++) {
            ll maxc = s[i] - '0';
            ll maxi = i;
            for (int j = i + 1; j < n; j++) {
                if (s[j] - '0' + (i - j) > maxc) {
                    maxc = s[j] - '0' + (i - j);
                    maxi = j;
                }
            }

            swap(s[i], s[maxi]);
            s[i]+= (i - maxi);
        }

        cout << s << endl;
    }

    return 0;
}

Answer: 1


#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		vector<int> a;
		if(s.length() < 9){
			for(int i=0; i<s.length(); i++){
				a.push_back(s[i]-'0');
			}
		}
		else{
			for(int i=0; i<s.length(); i++){
				a.push_back(s[i]-'0');
			}
		}
	//	vector<int> a = {9, 8, 7, 6, 5, 4 ,3, 2, 1};
		vector<int> result;
		int size = a.size();
		for(int j=0; j<size; j++){
			int max = a[0];
			int track = 0;
			for(int i=0; i<a.size(); i++){
				if(max < a[i] - i){
					max = a[i] - i;
					track = i;
				}
			}
			result.push_back(max);
			a.erase(a.begin() + track);

		}
		for(int i=0; i<result.size(); i++){
			cout<<result[i];
		}
		if(s.length() > 9){
			for(int i=10; i<s.length(); i++){
				cout<<s[i];
			}
		}
		cout<<endl;
	}

}

#include<iostream>
using namespace std;
int main() {
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		for(int i=0; i<s.length(); i++){
			if(s[i] < s[i+1]-1){
				swap(s[i], s[i+1]);
				s[i]--;
				if(i>=1){
					i-=2;
				}
			}
		}
		cout<<s<<endl;
	}
}

Answer: 2


#include <iostream>
#include <algorithm>
#include <limits>
#include <map>
#include <set>
#include <vector>
#define vi vector<int>
#define pb push_back
#define int long long
using namespace std;
const int N = 5e5+10;
int t,sum=0;
string s;

void solve(){
	cin>>s;
	for(int i=0;i<s.length();i++){
		for(int j=i;j>=1;j--){
			if(s[j-1]<s[j]-1){
				s[j]=(s[j]-'0')-1+'0';
				char w=s[j];
				s[j]=s[j-1];
				s[j-1]=w;
			}
		}
	}
	cout<<s<<endl;
}

signed main(void){
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--)
	solve();
	return 0;
}

#include <iostream>
#include <algorithm>
#include <limits>
#include <map>
#include <set>
#include <vector>
#define vi vector<int>
#define pb push_back
#define int long long
using namespace std;
const int N = 5e5+10;
int t,sum=0;
string s;

void solve(){
	cin>>s;
	for(int i=0;i<s.length();i++){
		for(int j=i;j>=1;j--){
			if(s[j-1]<s[j]-1){
				s[j]=(s[j]-'0')-1+'0';
				char w=s[j];
				s[j]=s[j-1];
				s[j-1]=w;
			}
			else{
				break;
			}
		}
	}
	cout<<s<<endl;
}

signed main(void){
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--)
	solve();
	return 0;
}

Answer: 2


#include "bits/stdc++.h"

#define int long long

using namespace std;

void A()

{

	string s;

	cin >>s;

	map<int,int> mp;

	int n=s.size(),i,j;

	int d=0;

	for(int p=1;p<=50000;p++)

	{

	for(j=n-1,i=n-1;i>0;i--)

	{

		j=i;

		if(s[i]-1>s[i-1])

		{

			char q;

			q=s[i]-1;

			s[i]=s[i-1];

			s[i-1]=q;

		}

	}

	}

	cout <<s<<endl;

}

signed main()

{

	int t;

	cin >>t;

	while(t--) A();

	return 0;

}

#include "bits/stdc++.h"

#define int long long

using namespace std;

void A()

{

	string s;

	cin >>s;

	map<int,int> mp;

	int n=s.size(),i,j;

	int d=0;

	for(int p=1;p<=25000;p++)

	{

	for(j=n-1,i=n-1;i>0;i--)

	{

		j=i;

		if(s[i]-1>s[i-1])

		{

			char q;

			q=s[i]-1;

			s[i]=s[i-1];

			s[i-1]=q;

		}

	}

	}

	cout <<s<<endl;

}

signed main()

{

	int t;

	cin >>t;

	while(t--) A();

	return 0;

}

Answer: 2


#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int main(){
    int t;
    cin >> t;
    vector<string> answers;
    while(t--){
        string s;
        cin >> s;
        int n=s.size();
        vector<int> newvalues;
        for(int i =0;i<n-1; i++){
            newvalues.clear();
            for(int m=0; m<n; m++){
            newvalues.push_back((s[m]-'0')-m+i);
            }

            int max=0;
            int toswapindex=0;
            for(int j=i+1;j<n;j++){
                if(s[i]-'0'<newvalues[j]){
                    int cur = newvalues[j];
                    if(cur>max){
                        max=cur;
                        toswapindex=j;
                    }
                }
            }
            if(max!=0){
                for(int k=toswapindex; k>i;k--){
                    s[k]=s[k-1];
                }
                s[i]= max+'0';
            }
        }
        answers.push_back(s);
    }
    for(string answer : answers){
        cout << answer << "\n";
    }
    return 0;
}

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int main(){
    int t;
    cin >> t;
    vector<string> answers;
    while(t--){
        string s;
        cin >> s;
        int n=s.size();
        vector<long long> newvalues;
        for(int i =0;i<n-1; i++){
            newvalues.clear();

            for(int m=i; m<min(i + 10, n); m++){
            newvalues.push_back((s[m]-'0')-m+i);
            }

            int max=0;
            int toswapindex=0;
            for(int j=i+1;j<min(i + 10, n);j++){
                if(s[i]-'0'<newvalues[j-i]){
                    int cur = newvalues[j-i];
                    if(cur>max){
                        max=cur;
                        toswapindex=j;
                    }
                }
            }
            if(max!=0){
                for(int k=toswapindex; k>i;k--){
                    s[k]=s[k-1];
                }
                s[i]= max+'0';
            }
        }
        answers.push_back(s);
    }
    for(string answer : answers){
        cout << answer << "\n";
    }
    return 0;
}

Answer: 2


#include <iostream>

using namespace std;

bool check(string a)

{

	for (int i = 0; i < a.size() - 1; ++i)

	{

		if (a[i + 1] == '0')

		{

			continue;

		}

		if (a[i] + 1 < a[i + 1])

		{

			return true;

		}

	}

	return false;

}

int main()

{

	int t;

	cin >> t;

	while (t--)

	{

		string s;

		cin >> s;

		while (check(s) == true)

		{

			for (int i = 0; i < s.size() - 1; ++i)

			{

				if (s[i] == '0')

				{

					continue;

				}

				if (s[i] + 1 < s[i + 1])

				{

					s[i + 1] -= 1;

					char temp = s[i];

					s[i] = s[i + 1];

					s[i + 1] = temp;

				}

			}

		}

		cout << s << endl;

	}

	return 0;

}

#include <iostream>

using namespace std;

bool check(string a)

{

	for (int i = 0; i < a.size() - 1; ++i)

	{

		if (a[i + 1] == '0')

		{

			continue;

		}

		if (a[i] + 1 < a[i + 1])

		{

			return true;

		}

	}

	return false;

}

int main()

{

	int t;

	cin >> t;

	while (t--)

	{

		string s;

		cin >> s;

		while (check(s) == true)

		{

			for (int i = 0; i < s.size() - 1; ++i)

			{

				if (s[i + 1] == '0')

				{

					continue;

				}

				if (s[i] + 1 < s[i + 1])

				{

					s[i + 1] -= 1;

					char temp = s[i];

					s[i] = s[i + 1];

					s[i + 1] = temp;

				}

			}

		}

		cout << s << endl;

	}

	return 0;

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N=1e5;

int32_t main() {

	ios::sync_with_stdio(0);

	cin.tie(0);

	ll t;

	cin >> t;

	while (t--) {

		string s;

		cin>>s;

		ll n=s.size();

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

			int in=0,idx=0;

			for(int j=i;j<min(n,i+10);j++){

				if(s[j]-j+i-'0'>in) {in=s[j]-j+i-'0'; idx=j;}

			}

			if(in!=s[i]-'0') {

				for(int j=idx-1;j>=i;j--){

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

				}

				s[i]=char(in+'0');

			}

		}

		cout<<s<<"\n";

	}

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define vi vector<int>

#define vvi vector<vector<int>>

#define vll vector<long long>

#define ll long long

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

#define mod 1000000007

void solve(){

    string s; cin >> s;

    int n = s.size();

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

        int maxi = 0, ind = i;

        for(int j=i; j < min(i+9, n); j++){

            if(maxi < (s[j]-'0')+(i-j)){

                maxi = (s[j]-'0')+(i-j);

                ind = j;

            }

        }

        //cout << ind << " ";

        while(ind > i){

            swap(s[ind], s[ind-1]);

            ind--;

        }

        s[i] = '0'+maxi;

    }

    cout << s << endl;

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    int tt = (cin >> tt, tt);

    while(tt--) solve();

}

Answer: 1


#include<bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)

#define rrep(i, a, b) for (int i = a; i >= (b); i--)

#define range(i, a, b, s) for (int i = a; (s > 0 && i < (b)) || (s < 0 && i > (b)); i += s)

#define trav(a, x) for (auto& a : x)

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

#define sz(x) (int) (x).size()

typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;

typedef vector<pii> vpi;

template <typename T>

void print_arr(vector<T> a) {

	trav(x, a) cout << to_string(x) << " ";

	cout << "\n";

}

int T;

vi a;

string s;

int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin >> T;

	while (T--) {

		cin >> s;

		vi skip(sz(s), 0);

		rep(i, 0, sz(s)) {

			if (skip[i]) continue;

			int mx = s[i] - '0';

			int mx_pos = i;

			int skipped = 0;

			rep(j, 1, 20) {

				if (i+j >= sz(s)) break;

				if (skip[i+j]) {

					skipped++;

					continue;

				}

				if (s[i+j] - '0' - j + skipped > mx) {

					//cout << i << " " << i+j << " " << s[i+j] - '0' - j << '\n';

					mx = s[i+j] -'0' - j + skipped;

					mx_pos = i+j;

				}

			}

			cout << mx;

			skip[mx_pos] = 1;

			i--;

		}

		cout << '\n';

	}

	return 0;

}

#include<bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); i++)

#define rrep(i, a, b) for (int i = a; i >= (b); i--)

#define range(i, a, b, s) for (int i = a; (s > 0 && i < (b)) || (s < 0 && i > (b)); i += s)

#define trav(a, x) for (auto& a : x)

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

#define sz(x) (int) (x).size()

typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;

typedef vector<pii> vpi;

template <typename T>

void print_arr(vector<T> a) {

	trav(x, a) cout << to_string(x) << " ";

	cout << "\n";

}

int T;

vi a;

string s;

int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin >> T;

	while (T--) {

		cin >> s;

		vi skip(sz(s), 0);

		rep(i, 0, sz(s)) {

			if (skip[i]) continue;

			int mx = s[i] - '0';

			int mx_pos = 0;

			rep(j, 1, 10) {

				if (i+j >= sz(s)) break;

				if (s[i+j] - '0' - j > mx) {

					//cout << i << " " << i+j << " " << s[i+j] - '0' - j << '\n';

					mx = s[i+j] -'0' - j;

					mx_pos = j;

				}

			}

			char prev = s[i];

			rep(j, 1, mx_pos+1) {

				if (i+j >= sz(s)) break;

				swap(s[i+j], prev);

			}

			s[i] = mx+'0';

		}

		cout << s << '\n';

	}

	return 0;

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void solve() {
    string s; cin >> s;
    for (int i = 0; i < s.size(); i++) {
        int best = s[i] - '0', pos = i;
        for (int j = i; j < min(i + 10, (int) s.size()); j++) {
            if (s[j] - '0' - (j - i) > best) {
                best = s[j] - '0' - (j - i);
                pos = j;
            }
        }
        while (pos > i) {
            swap(s[pos], s[pos - 1]);
            pos--;
        }
        s[i] = char(best + '0');
    }
    cout << s<<endl;
}

int main() {
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void solve() {
    string s; cin >> s;
    for (int i = 0; i < s.size(); i++) {
        int best = s[i] - '0', pos = i;
        for (int j = i; j < min(i + 10, (int) s.size()); j++) {
            if (s[j] - '0' - (j - i) > best) {
                best = s[j] - '0' - (j - i);
                pos = j;
            }
        }
        while (pos > i) {
            swap(s[pos], s[pos - 1]);
            pos--;
        }
        s[i] = char(best + '0');
    }
    cout << s;
}

int main() {
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
#include "debug.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i,j;
    for (int k = 0; k < s.length() + 1; k++)
    {
        if((s[k+1] - s[k]) > 1){
            i = k;j = k+1;
            do
            {
                s[j]--;
                // cout<<"s[j] "<<s[j]<<endl;
                swap(s[i],s[j]);
                i--;j--;
                // cout<<"cod "<<( i > 0 && j > 0 && ((s[j] - s[i]) > 1))<<endl;
            } while ( i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));

        }
    }
    cout<<s<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i, j;
    for (int k = 0; (k + 1) < s.length(); k++)
    {
        if ((s[k + 1] - s[k]) > 1)
        {
            i = k;
            j = k + 1;
            do
            {
                swap(s[i], --s[j]);
                i--; j--;
            } while (i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));
        }
    }
    cout << s << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

Answer: 2


#include <iostream>
#include <string>
#include <vector>
using namespace std;

void solve()
{
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		int k = i;
		for (int j = i; j < n && j < i + 10; j++)
		{
			if (s[j] - j > s[k] - k)
			{
				k = j;
			}
		}

		while (k > i)
		{
			s[k]--;
			swap(s[k - 1], s[k]);
			k--;
		}
	}

	cout << s << "\n";
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void solve()
{
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n-1; i++)
	{
		int cur = s[i] - '0';
		vector<int> steps(n, 0);
		int step = 1;
		int maxv = 0, maxind = -1;
		for (int j = i + 1; j < n && j < i+10; j++)
		{
			steps[j] = s[j] == '0' ? 0 : (s[j] - '0') - step;
			if (steps[j] > maxv && steps[j] > cur)
			{
				maxv = steps[j];
				maxind = j;
			}
			step++;
		}

		int j;
		for (j = (maxind == -1 ? -1 : maxind); j > i; j--)
		{
			swap(s[j], s[j - 1]);
		}

		if (j != -1)
		{
			int t = s[j] - '0';
			t = steps[maxind];
			s[j] = t + '0';
		}
	}

	cout << s << "\n";
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}

Answer: 1


#include <ostream>

#include <cstdio>

#include <algorithm>

#include<list>

#include<queue>

#include <iostream>

#include <cstring>

#include <math.h>

#include <cmath>

#include <string>

using namespace std;

int t,max1=numeric_limits<int>::min(),a;

char s[1000000];

int main(){

    scanf("%d",&t);

    while (t--)

    {

        a=0;

        scanf("%s",s);

        for (int i = 0; i < strlen(s); i++)

        {

            for (int j = i; j < i+9; j++)

            {

                if ((s[j]-'0')-j+i > max1)

                {

                    max1 = (s[j]-'0')-j+i;

                    a = j;

                }

            }

            for (int b = a; b > i; b--)

            {

                s[b] = s[b-1];

            }

                s[i] = '0';

            printf("%d",max1);

            max1=numeric_limits<int>::min();

        }

        printf("\n");

    }

    return 0;

}

#include <ostream>

#include <cstdio>

#include <algorithm>

#include<list>

#include<queue>

#include <iostream>

#include <cstring>

#include <math.h>

#include <cmath>

#include <string>

using namespace std;

using i64 = long long;

int T, n;

void solve() {

    string s; cin >> s;

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

        int res = s[i], pos = i;

        for (int j = i + 1; j < min((int)s.size(), i + 11); j ++)

            if ((int)s[j] - (j - i) > res) res = (int)s[j] - (j - i), pos = j;

        for (int j = pos; j > i; j --) swap(s[j], s[j - 1]), s[j - 1] --;

    }

    cout << s << '\n';

}

int main() {

    cin.tie(NULL) -> sync_with_stdio(false);

    cin >> T;

    while (T --) solve();

    return 0;

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int find(string &s, int r)
{
    int l = r;
    while (l >= 0)
    {
        if (s[r] - s[l] > 1 || s[l] - s[r] > 1)
        {
            return l;
        }
        else
        {
            l--;
        }
    }
    return l;
}

void change(string &s, int r)
{
    int l = r;
    while (l < s.length() - 1)
    {
        if (s[l + 1] - s[l] > 1)
        {
            char temp = s[l + 1];
            s[l + 1] = s[l];
            s[l] = temp - 1;
            l++;
            // cout << s << " s from change" << endl;
        }
        else
        {
            return;
        }
    }
    return;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int r = s.size() - 1;
        while (r >= 0)
        {
            if (s[r] == '0')
            {
                r--;
            }
            else
            {
                int k;
                k = find(s, r);
                // cout << k << endl;
                if (r >= 0)
                {
                    change(s, k);
                }
                r = k;
                // cout << r << "next" << " st " << s << endl;
            }
        }
        cout << s << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int find(string &s, int r)
{
    int l = r-1;
    // cout << l << endl;
    while (l >= 0)
    {
        if (s[r] - s[l] > 1 || s[l] - s[r] > 1)
        {
            return l;
        }
        else
        {
            l--;
            r--;
        }
    }
    return l;
}

void change(string &s, int r)
{
    int l = r;
    while (l < s.length() - 1)
    {
        if (s[l + 1] - s[l] > 1)
        {
            char temp = s[l + 1];
            s[l + 1] = s[l];
            s[l] = temp - 1;
            l++;
            // cout << s << " s from change" << endl;
        }
        else
        {
            return;
        }
    }
    return;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int r = s.size() - 1;
        while (r >= 0)
        {
            if (s[r] == '0')
            {
                r--;
            }
            else
            {
                int k;
                k = find(s, r);
                // cout << k << endl;
                if (k >= 0)
                {
                    change(s, k);
                }
                r = k;
                // cout << r << "next" << " st " << s << endl;
            }
        }
        cout << s << endl;
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 5;
string s;
ll t;
int main(){
	cin >> t;
	while(t--){
		cin >> s;
		for(int i = 0; i < s.size(); i++){
			ll maxx = 0, k = i;
			for(int j = i + 1; j <= i + 10 && j < s.size(); j++){
				if((s[j] - '0') - j + i - (s[i] - '0') > maxx){
					maxx = (s[j] - '0') - j + i - (s[i] - '0');
					k = j;
				}
			}
			swap(s[i], s[k]);
			s[i] = s[i] - k + i;
		}
		cout << s << endl;
	}
	return 0;
}

Answer: 1


#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

#define int long long

typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

signed main(){

int t;

cin>>t;

while(t--){

string n;

cin>>n;

vector<char>a;

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

a.push_back(n[i]);

}

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

//     cout<<a[i]<<' ';

// }

// cout<<endl;

// reverse(a.begin(),a.end());

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

int y=i;

int x=i;

while(((a[i])>(a[x-1]+('1'-'0')))&&(x>0)){

a[i]-=('1'-'0');

//cout<<a[i]<<' '<<a[x-1]<<endl;

swap(a[i],a[x-1]);

x--;

i--;

}

y=i;

}

// for(int i=0;i<)

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

cout<<a[i];

}

cout<<endl;

}

}

#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

#define int long long

typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

// bool comp(int &a,int&b){

//     if(a>b+1){

//         a--;

//         return a>b;

//     }

// }

signed main(){

int t;

cin>>t;

while(t--){

int n;

cin>>n;

int x=n;

vector<int>a;

if(x==0){

cout<<0<<endl;

}

else{

while(x!=0){

a.push_back(x%10);

x/=10;

}

reverse(a.begin(),a.end());

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

int y=i;

int x=i;

while((a[i]>a[x-1]+1)&&(x>0)){

a[i]--;

//cout<<a[i]<<' '<<a[x-1]<<endl;

swap(a[i],a[x-1]);

x--;

i--;

}

y=i;

}

// for(int i=0;i<)

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

cout<<a[i];

}

cout<<endl;

}

}

}

Answer: 1


t = int(input())
for _ in range(t):
    num = list(input())  # Convert string to list for mutable operations
    n = len(num)
    for i in range(n - 1):
        j = i + 1
        while j < n and (int(num[j]) - 1 > int(num[j - 1])):
            temp = str(int(num[j]) - 1)
            num[j] = num[j - 1]
            num[j - 1] = temp
            j -= 1  # Move backward to recheck the order
            if j <= 0:  # Prevent going out of bounds
                break
    print(''.join(num))  # Convert list back to string for output

t = int(input())
for _ in range(t):
    num = input()
    n = len(num)
    ans = ""
    for j in range(n-1):
        max_el = j
        for i in range(j+1, n):
            # print(num)
            # print(f"i: {i}, j: {j}")
            if(int(num[i]) == 0): continue
            if((int(num[i]) - (i - j)) > int(num[max_el]) - (max_el - j)):
                max_el = i
        if(max_el != j):
            num = num[:j]+str(int(num[max_el]) - (max_el - j))+num[j:]
            # print(f"num: {num}")
            num = num[:max_el+1] + num[max_el+2:]
    print(num)

Answer: 1


import java.math.*;

import java.util.*;

public class Main {

static int N = (int)2e5 + 5;

static int[] a = new int[N];

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int T = sc.nextInt();

while(T-- > 0){

String s = sc.next();

int len = s.length();

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

a[i] = (int)s.charAt(i - 1) - (int)'0';

}

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

int x = a[i], idx = i;

int mx = x, id = -1;

for(int j = i + 1;j <= len && j <= i + 8;j++){

if(a[j] - (j - i) > mx){

mx = a[j] - (j - i);

idx = j;

}

}

for(int j = idx - 1;j >= i;j--){

int t = a[j];

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

a[j + 1] = t;

}

}

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

System.out.print(a[i]);

}

System.out.println();

}

sc.close();

}

}

import java.math.*;

import java.util.*;

public class Main {

static int N = (int)2e5 + 5;

static int[] a = new int[N];

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int T = sc.nextInt();

while(T-- > 0){

String s = sc.next();

int len = s.length();

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

a[i] = (int)s.charAt(i - 1) - (int)'0';

}

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

int x = a[i], idx = i;

int mx = 0, id = -1;

for(int j = i + 1;j <= len && j <= i + 8;j++){

if(a[j] - (j - i) > mx){

mx = a[j] - (j - i);

idx = j;

}

}

for(int j = idx - 1;j >= i;j--){

int t = a[j];

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

a[j + 1] = t;

}

}

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

System.out.print(a[i]);

}

System.out.println();

}

sc.close();

}

}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define pb push_back
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define alias cout << "Alias" << endl;
#define bob cout << "Bob" << endl;
const int MOD = 1e9+7;
const int INF = 1e18;

void solve() {
    string s; cin>>s;
    int n=s.size();
    vi arr;
    vi ans;
    int c=0;
    for(int i=0;i<min(10LL,n);i++){
        // cout << s[c+i] << " " << s[c+i]-'0'-i << endl;
        arr.pb(s[c+i]-'0'-i);
    }
    int val=*max_element(arr.begin(),arr.end());
    while(!arr.empty()){
        ans.pb(val);
        int i=0;
        vi temp;
        while(arr[i] != val){
            temp.pb(arr[i]);
            i++;
        }
        i++;
        while(i<arr.size()){
            temp.pb(arr[i]+1);
            i++;
        }
        arr.clear();
        if(c+10 < n){
            temp.pb(s[c+10]-'0'-9);
            c++;
        }
        arr = temp;
        if(!arr.empty()) val = *max_element(arr.begin(),arr.end());
    }
    for(auto ele: ans) cout << ele << "";
    cout << endl;
}

int32_t main() {
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define pb push_back
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define alias cout << "Alias" << endl;
#define bob cout << "Bob" << endl;
const int MOD = 1e9+7;
const int INF = 1e18;

void solve() {
    string s; cin>>s;
    int n=s.size();
    vi arr;
    vi ans;
    int c=0;
    for(int i=0;i<min(10LL,n);i++){
        // cout << s[c+i] << " " << s[c+i]-'0'-i << endl;
        arr.pb(s[c+i]-'0'-i);
    }
    int val=*max_element(arr.begin(),arr.end());
    while(!arr.empty()){
        ans.pb(val);
        int i=0;
        vi temp;
        while(arr[i] != val){
            temp.pb(arr[i]);
            i++;
        }
        i++;
        while(i<arr.size()){
            temp.pb(arr[i]+1);
            i++;
        }
        arr.clear();
        if(c+10 < n){
            temp.pb(s[c+10]-9);
            c++;
        }
        arr = temp;
        if(!arr.empty()) val = *max_element(arr.begin(),arr.end());
    }
    for(auto ele: ans) cout << ele << "";
    cout << endl;
}

int32_t main() {
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
#define memclear(a) memset(a,0,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
using ll=long long;
int num[200050];
void solve(string s){
	int n=s.length();
	memclear(num);
	int i,j,maxn,maxi;
	for(i=0;i<n;i++){
		num[i]=s[i]-'0';
	}
	for(i=0;i<n;i++){
		maxn=num[i];
		maxi=i;
		for(int j=i+1;j<i+9&&j<=n;j++){
			int m=j-i;
			if(num[j]-m>maxn){
				maxn=num[j]-m;
				maxi=j;
			}
		}
		if(maxi!=i){
			for(int k=maxi;k>=i+1;k--){
				num[k]=num[k-1];
			}
			num[i]=maxn;
		}
	}
	for(int i=0;i<n;i++) cout<<num[i];
	cout<<"\n";
}
int main(){
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int t;
  cin>>t;
  while(t--){
		string s;
	  cin>>s;
	  solve(s);
	}
  return 0;
}

#include<bits/stdc++.h>
#define memclear(a) memset(a,0,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
using ll=long long;
int num[20050];
void solve(string s){
	int n=s.length();
	memclear(num);
	int i,j,maxn,maxi;
	for(i=0;i<n;i++){
		num[i]=s[i]-'0';
	}
	for(i=0;i<n;i++){
		maxn=num[i];
		maxi=i;
		for(int j=i+1;j<i+9&&j<=n;j++){
			int m=j-i;
			if(num[j]-m>maxn){
				maxn=num[j]-m;
				maxi=j;
			}
		}
		if(maxi!=i){
			for(int k=maxi;k>=i+1;k--){
				num[k]=num[k-1];
			}
			num[i]=maxn;
		}
	}
	for(int i=0;i<n;i++) cout<<num[i];
	cout<<"\n";
}
int main(){
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int t;
  cin>>t;
  while(t--){
		string s;
	  cin>>s;
	  solve(s);
	}
  return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;

void solution() {
    string s;cin>>s;
    int n=s.size();

    for(int i=0;i<n;i++){
        int maxi=(s[i]-'0'),ind=i;
        for(int j=0;j<=9;j++){
            if(i+j<n){
                if(s[i+j]-'0'-j>=maxi){
                    ind=i+j;
                    maxi=s[i+j]-'0';
                }
            }
            else{
                break;
            }
        }
        char ch=s[ind]-(ind-i);
        for(int j=ind;j>i;j--){
            s[j]=s[j-1];
        }
        s[i]=ch;
    }

    cout<<s<<endl;

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int Testcases = 1;
    cin >> Testcases;
    for (int i = 0; i < Testcases; i++) {
        solution();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;

void solution() {
    string s;cin>>s;
    int n=s.size();

    for(int i=0;i<n;i++){
        int maxi=(s[i]-'0'),ind=i;
        for(int j=1;j<=9;j++){
            if(i+j<n){
                if(s[i+j]-'0'-j>maxi){
                    ind=i+j;
                    maxi=s[i+j]-'0'-j;
                }
            }
            else{
                break;
            }
        }
        // cout<<maxi<<endl;
        char ch=s[ind]-(ind-i);
        for(int j=ind;j>i;j--){
            s[j]=s[j-1];
        }
        s[i]=ch;
    }

    cout<<s<<endl;

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int Testcases = 1;
    cin >> Testcases;
    for (int i = 0; i < Testcases; i++) {
        solution();
    }
    return 0;
}

Answer: 2


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int posi = 0;
        while (s[posi] != '\0')
        {
            if (s[posi + 1] > s[posi] + 1)
            {
                char temp = s[posi];
                int tempo = s[posi + 1] - '0' - 1;
                s[posi] = tempo + '0';
                s[posi + 1] = temp;
                if (posi > 0)
                {
                    int positemp = posi;
                    while (s[positemp] > s[positemp - 1] + 1)
                    {
                        char temp2 = s[positemp - 1];
                        int tempo2 = s[positemp] - '0' - 1;
                        s[positemp - 1] = tempo2 + '0';
                        s[positemp] = temp2;
                        positemp--;
                        if (positemp <= 0)
                        {
                            // posi++;
                            break;
                        }
                    }
                }
            }
            posi++;
        }
        cout << s << endl;
    }
}

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int posi = 0;
        while (s[posi] != '\0')
        {
            if (s[posi + 1]>s[posi]+1 )
            {
                char temp = s[posi];
                int tempo = s[posi + 1] - '0' - 1;
                s[posi] = tempo + '0';
                s[posi + 1] = temp;
                posi = 0;
                continue;
            }
            posi++;
        }
        cout << s << endl;
    }
}

Answer: 1


for _ in range(int(input())):
    s = list(map(int, list(input())))
    n = len(s)

    for i in range(1, n):
        if s[i] == 0:
            continue
        for j in range(max(0, i - 9), i):
            if s[j] < s[i] - (i - j):
                new_head_value = s[i] - (i - j)
                idx = i - 1
                while idx != j - 1:
                    s[idx], s[idx + 1] = s[idx + 1], s[idx]
                    idx -= 1
                s[j] = new_head_value
                break

    for i in range(n):
        print(s[i], end='')
    print()

for _ in range(int(input())):
    s = list(map(int, list(input())))
    n = len(s)
    prev_max = s[0]
    prev_max_i = 0

    for i in range(1, n):
        if s[i] == 0:
            continue
        for j in range(prev_max_i, i):
            if s[j] + (i - j) >= 9:
                prev_max_i = j
                prev_max = s[j]
            if s[j] < s[i] - (i - j):
                temp = s[i]
                s.insert(j, s[i] - (i - j))
                s.pop(i + 1)
                # prev_max_i = j
                # prev_max = s[j]
                break

        # print(s)

    for i in range(n):
        print(s[i], end='')
    print()

Answer: 1


t= int(input())
for k in range(t):
    a=list(input())
    b= len(a)
    for i in range(b):
        t= min(i+9,b)
        m= int(a[i])
        n=i
        for j in range(i,t):
            if (int(a[j])-j+i)> m:
                m= int(a[j])-j+i
                n= j
        while n>i:
            a[n],a[n-1]= a[n-1],str(int(a[n])-1)
            n-=1
    print("".join(a))

t= int(input())
for k in range(t):
    a=input()
    b= len(a)
    for i in range(b):
        t= min(i+9,b)
        m= int(a[i])
        n=i
        for j in range(i,t):
            if (int(a[j])-j+i)> m:
                m= int(a[j])-j+i
                n= j
        a= a[0:i]+str(m)+a[i:n]+a[n+1:b]
    print(a)

Answer: 1


#include<bits/stdc++.h>

using namespace std;

using ll = long long;

using ld = long double;

#define mod 998244353

void solve(){

    //code here

    string s;

    cin>>s;

    vector<pair<int, int>> v;

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

        int t = s[i]-'0';

        v.push_back({t-i, i});

    }

    sort(v.begin(), v.end(), [&](auto a, auto b) {

        if(a.first == b.first) return a.second>b.second;

        return a.first>b.first;

    });

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

        if(v[i].second<= i) {

            cout<<v[i].first + v[i].second;

        } else {

            cout<<v[i].first + i;

        }

    }

    return ;

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    ll t = 1;

    cin >> t;

    while (t--){

        // cout<<"-----------"<<endl;

        solve();

        cout<<"\n";

    }

    return 0;

}

#include<bits/stdc++.h>

using namespace std;

using ll = long long;

using ld = long double;

#define mod 998244353

void solve(){

    //code here

    string s; cin >> s;

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

        int tmp = s[i] - '0', pos = i;

        for (int j = i; j < min(i + 10, (int) s.size()); j++) {

            if (s[j] - '0' - (j - i) > tmp) {

                tmp = s[j] - '0' - (j - i);

                pos = j;

            }

        }

        while (pos > i) {

            swap(s[pos], s[pos - 1]);

            pos--;

        }

        s[i] = char(tmp + '0');

    }

    cout << s;

    return ;

}

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    ll t = 1;

    cin >> t;

    while (t--){

        // cout<<"-----------"<<endl;

        solve();

        cout<<"\n";

    }

    return 0;

}

Answer: 2


#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();
        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();
}

#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();
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define fori(i, a, b) for(int i = a; i < b; ++i)
#define rfori(i, a, b) for(int i = a; i >= b; --i)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define f first
#define s second
#define clr(a) memset(a, 0, sizeof(a))
#define set_mem(a, b) memset(a, b, sizeof(a))
#define sz(v) (int)v.size()
#define umap unordered_map
#define uset unordered_set
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define vvll vector<vll>
#define pq priority_queue
#define minpq(x) priority_queue<x, vector<x>, greater<x>>

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef unordered_map<int, int> umii;
typedef unordered_set<int> usi;

void solve()
{
    string s;
    cin >> s;
    ll n = s.length();

    for(ll i=1;i<n;i++){
        for(ll j=i;j>0;j--){
            if(s[j]-1>s[j-1]){
                swap(--s[j],s[j-1]);
            }
            else break;
        }
    }
    cout<<s<<endl;
    // string s;
    // cin >> s;
    // ll n = s.length();

    // for (ll i = 0; i < n; i++) {
    //     string temp = s;
    //     ll maxi = LONG_MIN;
    //     ll idx = i;

    //     for (ll j = i + 1; j < n; j++) {
    //         if ((s[j] - '0'-(j-i)) > maxi-(idx-i) && idx>=0) {
    //             maxi = s[j] - '0';
    //             idx = j;
    //         }
    //     }

    //     if (maxi - (idx-i)> (s[i] - '0')) {

    //         char maxChar = s[idx]-(idx-i);
    //         for (ll j = idx; j > i; j--) {
    //             s[j] = s[j - 1];
    //         }
    //         s[i] = maxChar;
    //     }
    // }

    // cout << s << endl;

    // ll maxi = INT_MIN;
    // int cnt = 1;
    // vector<pair<int,int>> maxArr(n,{-1,cnt});

    // for(int i=n-1;i>=0;i--){
    //     if(maxi == s[i]-'0') cnt++;
    //     if(s[i]-'0'>maxi) maxi = s[i]-'0';

    //     maxArr[i].f = max(s[i]-'0',maxi);
    //     maxArr[i].s = cnt;
    // }
    // for(auto it:maxArr){
    //     cout<<it.f<<","<<it.s<<" ";

    // }
    // cout<<endl;
}

int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define fori(i, a, b) for(int i = a; i < b; ++i)
#define rfori(i, a, b) for(int i = a; i >= b; --i)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define f first
#define s second
#define clr(a) memset(a, 0, sizeof(a))
#define set_mem(a, b) memset(a, b, sizeof(a))
#define sz(v) (int)v.size()
#define umap unordered_map
#define uset unordered_set
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vi>
#define vvll vector<vll>
#define pq priority_queue
#define minpq(x) priority_queue<x, vector<x>, greater<x>>

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef unordered_map<int, int> umii;
typedef unordered_set<int> usi;

void solve()
{
    string s;
    cin >> s;
    ll n = s.length();

    for (ll i = 0; i < n; i++) {
        string temp = s;
        ll maxi = LONG_MIN;
        ll idx = i;

        for (ll j = i + 1; j < n; j++) {
            if ((s[j] - '0'-(j-i)) > maxi-(idx-i) && idx>=0) {
                maxi = s[j] - '0';
                idx = j;
            }
        }

        if (maxi - (idx-i)> (s[i] - '0')) {

            char maxChar = s[idx]-(idx-i);
            for (ll j = idx; j > i; j--) {
                s[j] = s[j - 1];
            }
            s[i] = maxChar;
        }
    }

    cout << s << endl;

    // ll maxi = INT_MIN;
    // int cnt = 1;
    // vector<pair<int,int>> maxArr(n,{-1,cnt});

    // for(int i=n-1;i>=0;i--){
    //     if(maxi == s[i]-'0') cnt++;
    //     if(s[i]-'0'>maxi) maxi = s[i]-'0';

    //     maxArr[i].f = max(s[i]-'0',maxi);
    //     maxArr[i].s = cnt;
    // }
    // for(auto it:maxArr){
    //     cout<<it.f<<","<<it.s<<" ";

    // }
    // cout<<endl;
}

int main() {
    fast_io;

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);

int tc;

cin>>tc;

string s;

while(tc--){

cin>>s;

string a = "";

int mx, itr;

while(s.size()) {

mx = -1;

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

if ((s[i]-'0')-i>mx) {

mx = (s[i]-'0') - i;

itr = i;

}

}

a = a + to_string(mx);

s.erase(s.begin()+ itr);

}

cout<<a<<'\n';

}

return 0;

}

#include <bits/stdc++.h>

using namespace std;

int32_t main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);

int tc;

cin>>tc;

string s;

while(tc--){

cin>>s;

int a;

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

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

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

a = s[j]-1;

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

s[j-1] = a;

}else break;

}

}

cout<<s<<'\n';

}

return 0;

}

Answer: 2


#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();

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

    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

#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();

    for (int i = 1; i < n; ++i)
    {
        for (int j = i; j > 0 && s[j] - s[j - 1] > 1; --j)
        {
            s[j]--;
            swap(s[j], s[j - 1]);
        }
    }

    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

Answer: 2


#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        for (int i = 0; i < s.size(); i++) {
            int best = s[i] - '0', pos = i;
            for (int j = i; j < min(i + 10, (int)s.size()); j++) {
                if (s[j] - '0' - (j - i) > best) {
                    best = s[j] - '0' - (j - i);
                    pos = j;
                }
            }
            while (pos > i) {
                swap(s[pos], s[pos - 1]);
                pos--;
            }
            s[i] = char(best + '0');
        }
        cout << s << '\n';
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        string ans;
        int cnt=0;
        while(ans.size()!=s.size()){
            int maxi=0;
            int index;
        for(int i=cnt;i<s.size();++i){
            if((s[i]-'0')-(i-cnt)>maxi){
                maxi=((s[i]-'0')-(i-cnt));
                index=i;
            }
        }
        while(index>cnt){
            swap(s[index],s[index-1]);
            index--;
        }
        ans=ans+to_string(maxi);
        cnt++;
        }
        cout<<ans<<endl;
    }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
//#define node pair< ll , pair<int , int > >

ll mod = 1000000007 , sz = 1e6 + 10;
bool sortBF(pair<int,int> a , pair<int,int> b){return a.first>b.first;}
bool sortdec(ll a,ll b){return a >= b ;}

///----------------------------------------------------------------------------

int lg (ll x){
    return __builtin_clzll(1) - __builtin_clzll(x);
}

///----------------------------------------------------------------------------

ll fPower(ll x, ll y)
{
     x %= mod;
     y %= mod;
     ll res = 1;
     while (y > 0) {
         if (y % 2 == 1)
            res = (res * x)%mod;
         y = y >> 1;
         x = (x * x);
         x %= mod;
    }
    return (res%mod);
}

///----------------------------------------------------------------------------

int main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(0);
    //freopen("tictactoe.in" , "r" , stdin);
    //freopen("output.txt" , "w" , stdout);
    int tt = 1 , tc = 1;
    cin>>tt;
    while(tt--){
        string s;cin>>s;
        int n = s.size();
        int arr[n];
        for (int i = 0 ; i < n ; i++)arr[i] = (s[i] - '0');
        set<int>idx;
        for (int i = 0 ; i < min(9 , n) ; i++)idx.insert(i);
        int ans [n];
        for (int i = 0 ; i < n ; i++){
            int j = 0 , bestIdx = 0 , bestVal = -1;
            for (auto v : idx){
                if (arr[v] - j > bestVal){
                    bestVal = arr[v] - j;
                    bestIdx = v;
                }
                j++;
            }
            idx.erase(bestIdx);
            ans[i] = bestVal;
            if (i + 9 < n)idx.insert(i + 9);
        }
        for (auto i : ans)cout<<i;
        cout<<'\n';
    }
}

///nCr method
/*
vector <vector<ll>>memo(1001 , vector<ll>(1001 , -1));

ll nCr(ll n, ll r){
    if (r > n)
        return 0;
    if (r == 0 || r == n)
        return 1;
    if (memo[n][r] > -1)return memo[n][r];
    memo[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r);
    return memo[n][r];
}
*/

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
//#define node pair< ll , pair<int , int > >

ll mod = 1000000007 , sz = 1e6 + 10;
bool sortBF(pair<int,int> a , pair<int,int> b){return a.first>b.first;}
bool sortdec(ll a,ll b){return a >= b ;}

///----------------------------------------------------------------------------

int lg (ll x){
    return __builtin_clzll(1) - __builtin_clzll(x);
}

///----------------------------------------------------------------------------

ll fPower(ll x, ll y)
{
     x %= mod;
     y %= mod;
     ll res = 1;
     while (y > 0) {
         if (y % 2 == 1)
            res = (res * x)%mod;
         y = y >> 1;
         x = (x * x);
         x %= mod;
    }
    return (res%mod);
}

///----------------------------------------------------------------------------

int main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(0);
    //freopen("tictactoe.in" , "r" , stdin);
    //freopen("output.txt" , "w" , stdout);
    int tt = 1 , tc = 1;
    cin>>tt;
    while(tt--){
        string s;cin>>s;
        int n = s.size();
        int arr[n];
        for (int i = 0 ; i < n ; i++)arr[i] = (s[i] - '0');
        set<int>idx;
        for (int i = 0 ; i < min(9 , n) ; i++)idx.insert(i);
        int ans [n];
        for (int i = 0 ; i < n ; i++){
            int j = 0 , bestIdx = 0 , bestVal = 0;
            for (auto v : idx){
                if (arr[v] - j > bestVal){
                    bestVal = arr[v] - j;
                    bestIdx = v;
                }
                j++;
            }
            idx.erase(bestIdx);
            ans[i] = bestVal;
            if (i + 9 < n)idx.insert(i + 9);
        }
        for (auto i : ans)cout<<i;
        cout<<'\n';
    }
}

///nCr method
/*
vector <vector<ll>>memo(1001 , vector<ll>(1001 , -1));

ll nCr(ll n, ll r){
    if (r > n)
        return 0;
    if (r == 0 || r == n)
        return 1;
    if (memo[n][r] > -1)return memo[n][r];
    memo[n][r] = nCr(n - 1, r - 1) + nCr(n - 1, r);
    return memo[n][r];
}
*/

Answer: 1


//trying :-}

//      (????????????
//       ~  ?? ~

#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <unordered_set>

#include <algorithm>

#include <numeric>

#include <cmath>

#include <map>

using namespace std;

using ll = long long;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);

#define f(i, a, b) for (int i = a; i < b; ++i)

#define pb push_back

void solve() {

string s;

cin>>s;

int n=s.length();

vector<int> a(n);

f(i,0,n) a[i]=s[i]-'0';

f(i,0,n){

int k=i;

f(j,i,n){

if(a[k]-k<a[j]-j){

k=j;

}

}

while(k>i){

a[k]-=1;

int t=a[k];

a[k]=a[k-1];

a[k-1]=t;

k--;

}

}

f(i,0,n) cout<<a[i];

cout<<"\n";

}

int main() {

fastio;

int t;

cin >> t;

while (t--) solve();

return 0;

}

//  :-

//trying :-}

//      (????????????
//       ~  ?? ~

#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <unordered_set>

#include <algorithm>

#include <numeric>

#include <cmath>

#include <map>

using namespace std;

using ll = long long;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);

#define f(i, a, b) for (int i = a; i < b; ++i)

#define pb push_back

void solve() {

string a;

cin>>a;

int n=a.size();

f(i,0,n){

int k=i;

f(j,i,min(n,i+10)){

if(a[k]-k<a[j]-j){

k=j;

}

}

while(k>i){

a[k]-=1;

char t=a[k];

a[k]=a[k-1];

a[k-1]=t;

k--;

}

}

f(i,0,n) cout<<a[i];

cout<<"\n";

}

int main() {

fastio;

int t;

cin >> t;

while (t--) solve();

return 0;

}

//  :-

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=1e6+7;
int n,t,m;
string b;
ll a[MAXN];
int main()
{

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

	cin>>t;
	while(t--)
	{
		cin>>b;
		n=b.size();
		b=' '+b;
		for(int i=1;i<=n;i++) a[i]=b[i]-'0';
		for(int nowl=1;nowl<=n;nowl++)
		{
			int maxl=0,max_id=-1;
			for(int i=nowl;i<=min(n,nowl+10);i++)
			{
				int sss=a[i]-(i-nowl);
				// if(sss<0) break;
				if(sss>maxl) maxl=sss,max_id=i;
			}
			for(int i=max_id-1;i>=nowl;i--) swap(a[i],a[i+1]);
			a[nowl]=maxl;
		}
		for(int i=1;i<=n;i++) cout<<a[i];cout<<'\n';
	}
    return 0;
}
/*

1
5
1 2 3 5 4
1 2 3 4 5
A 1 3 1
A 1 3 1
A 1 3 1
A 1 3 1
*/

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=1e6+7;
int n,t,m;
string b;
ll a[MAXN];
int main()
{

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

	cin>>t;
	while(t--)
	{
		cin>>b;
		n=b.size();
		b=' '+b;
		for(int i=1;i<=n;i++) a[i]=b[i]-'0';
		for(int nowl=1;nowl<=n;nowl++)
		{
			int maxl=0,max_id=-1;
			for(int i=nowl;i<=n;i++)
			{
				int sss=a[i]-(i-nowl);
				if(sss<0) break;
				if(sss>maxl) maxl=sss,max_id=i;
			}
			for(int i=max_id-1;i>=nowl;i--) swap(a[i],a[i+1]);
			a[nowl]=maxl;
		}
		for(int i=1;i<=n;i++) cout<<a[i];cout<<'\n';
	}
    return 0;
}
/*

1
5
1 2 3 5 4
1 2 3 4 5
A 1 3 1
A 1 3 1
A 1 3 1
A 1 3 1
*/

Answer: 1


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f first
#define s second
int32_t main()
{
    fast_io
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        int n=a.size();
        for(int i=0;i<n-1;i++)
        {
            int j=min(i+9, n-1);
            int ij=i, mx=0;
            while(j!=i)
            {
                if(a[j]-(j-i)>a[i] && a[j]-(j-i)>mx)
                {
                    mx=a[j]-(j-i);
                    ij=j;
                }
                j--;
            }
            if(ij!=i){a[ij]-=(ij-i);
            swap(a[ij], a[i]);}
        }
        cout<<a<<'\n';
    }
    return 0;
}

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define f first
#define s second
int32_t main()
{
    fast_io
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        int n=a.size();
        for(int i=0;i<n-1;i++)
        {
            int j=min(i+9, n-1);
            int ij=i, mx=0;
            while(j!=i)
            {
                if(i==j)break;
                if(a[j]-(j-i)>a[i] && a[j]-(j-i)>=mx)
                {
                    mx=a[j]-(j-i);
                    ij=j;
                }
                j--;
            }
            if(ij!=i)
            {
                while(1)
                {
                    if(ij==i)break;
                    a[ij]--;
                    swap(a[ij-1], a[ij]);
                    ij--;
                    }
            }
        }
        cout<<a<<'\n';
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int main () {
	int t; cin >> t;
	while (t--) {
		string s;
		cin >> s;
		int n = s.size();
		int a[n];
		for (int i = 0; i < n; i++) {
			a[i] = s[i] - '0' - i;
		}
		for (int i = 0; i < n; i++) {
			int m = a[i], im = i;
			for (int j = i; j < n and j < i + 10; j++) {
				if (a[j] > m) {
					im = j, m = a[j];
				}
			}
			int t, t1 = m + 1;
			for (int j = i; j <= im; j++) {
				t = a[j];
				a[j] = t1 - 1;
				t1 = t;
			}
		}
		for (int i = 0; i < n; i++)
			cout << a[i] + i;
		cout << endl;
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main () {
	int t; cin >> t;
	while (t--) {
		string s;
		cin >> s;
		int n = s.size();
		int a[n];
		for (int i = 0; i < n; i++) {
			a[i] = s[i] - '0' - i;
		}
		for (int i = 0; i < n; i++) {
			int m = a[i], im = i;
			for (int j = i + 1; j < n; j++) {
				if (a[j] > m) {
					im = j, m = a[j];
				}
			}
			int t, t1 = m + 1;
			for (int j = i; j <= im; j++) {
				t = a[j];
				a[j] = t1 - 1;
				t1 = t;
			}
		}
		for (int i = 0; i < n; i++)
			cout << a[i] + i;
		cout << endl;
	}
	return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define out(x) #x << " = " << x << " "
#ifndef LOCAL
#define endl "\n"
#define cerr \
	if (false) cerr
#define stdio(true) stdio(false)
#endif

const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 10;

void solve() {
	string x;
	cin >> x;

	for (int i = 1; i < x.size(); i++) {
		int prv = 0;
		while (i - prv - 1 >= 0 && x[i - prv] != '0' && x[i - prv] - 1 > x[i - prv - 1]) {
			x[i - prv] -= 1;
			swap(x[i - prv], x[i - prv - 1]);
			prv++;
		}
	}

	cout << x << endl;
}

int main() {
	ios_base::sync_with_stdio(true);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define out(x) #x << " = " << x << " "
#ifndef LOCAL
#define endl "\n"
#define cerr \
	if (false) cerr
#define stdio(true) stdio(false)
#endif

const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 10;

bool isBigger(const string& a, const string& b) {
	if (a.size() > b.size()) return true;
	if (a.size() < b.size()) return false;

	return a > b;
}

void solve() {
	string x;
	cin >> x;

	int currentDigit = 1;

	while (currentDigit < x.size()) {
		if (currentDigit < 1) currentDigit = 1;
		if (x[currentDigit] == '0') {
			currentDigit++;
			continue;
		}

		string b = x;
		swap(b[currentDigit], b[currentDigit - 1]);
		b[currentDigit - 1]--;

		if (b > x) {
			swap(x[currentDigit], x[currentDigit - 1]);
			x[currentDigit - 1]--;
			currentDigit -= 2;
		}

		currentDigit++;
	}

	cout << x << endl;
}

int main() {
	ios_base::sync_with_stdio(true);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define cin(v) for (auto& i : v) cin >> i;
#define cout(v) for (auto& i : v) cout << i << " "; cout << endl;
#define endl "\n"
#define aint(a) a.begin(), a.end()
#define int long long
const int MOD = 1000000007;

void myprog();

int32_t main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t = 1;
        cin >> t;
        while (t-- > 0)
                myprog();
        return 0;
}

void myprog(){
        string s;
        cin >> s;

        int n = s.size();
        for(int i = 1; i < n; i++){
                for(int j = i; j >= 1; j--){
                        if(s[j] > s[j - 1] + 1){
                                swap(s[j], s[j - 1]);
                                s[j - 1]--;
                                //cout << s << " ";
                        }
                }
        }

        cout << s << endl;
}

#include <bits/stdc++.h>

using namespace std;

#define cin(v) for (auto& i : v) cin >> i;
#define cout(v) for (auto& i : v) cout << i << " "; cout << endl;
#define endl "\n"
#define aint(a) a.begin(), a.end()
#define int long long
const int MOD = 1000000007;

void myprog();

int32_t main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t = 1;
        cin >> t;
        while (t-- > 0)
                myprog();
        return 0;
}

void myprog(){
        string s;
        cin >> s;

        int n = s.size();
        for(int i = 1; i < n; i++){
                for(int j = i; j >= 1; j--){
                        if(s[j] > s[j - 1] + 1){
                                swap(s[j], s[j - 1]);
                                s[j - 1]--;
                                //cout << s << " ";
                        }
                        else{
                                break;
                        }
                }
        }

        cout << s << endl;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);

int tc;

cin>>tc;

string s;

while(tc--){

cin>>s;

int a;

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

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

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

a = s[j]-1;

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

s[j-1] = a;

}else break;

}

}

cout<<s<<'\n';

}

return 0;

}

#include <bits/stdc++.h>

using namespace std;

int32_t main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);

int tc;

cin>>tc;

string s;

while(tc--){

cin>>s;

string a = "";

int mx, itr;

while(s.size()) {

mx = -1;

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

if ((s[i]-'0')-i>mx) {

mx = (s[i]-'0') - i;

itr = i;

if (mx>9-i-1) break;

}

}

a = a + to_string(mx);

s.erase(s.begin()+ itr);

}

cout<<a<<'\n';

}

return 0;

}

Answer: 1


#include<iostream>
using namespace std;

void solve()
{
	string s;
	cin >> s;
	int n = s.size();
	for (int i = 0; i < n; i++)
	{
		int best = s[i] - '0', pos = i;
		for (int j = i; j < min(i + 10, (int)s.size()); j++) {
			if (s[j] - '0' - (j - i) > best) {
				best = s[j] - '0' - (j - i);
				pos = j;
			}
		}
		while (pos > i)
		{
			swap(s[pos], s[pos - 1]);
			pos--;
		}
		s[i] = char(best + '0');
	}
	cout << s<<endl;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}

}

#include<iostream>
using namespace std;

void solve()
{
	string s;
	cin >> s;
	int n = s.size(),j;
	for (int i = 0; i < n; i++)
	{
		int best = s[i] - '0', pos = i;
		for ( j = i; j < min(i + 10, n);j++);
		{
			if (s[j] - '0' - (j - i) > best)
			{
				best = s[j] - '0' - (j - i);
				pos = j;
		}
		}
		while (pos > i)
		{
			swap(s[pos], s[pos - 1]);
			pos--;
		}
		s[i] = char(best + '0');
	}
	cout << s<<endl;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define gc getchar_unlocked

#define fo(i, n) for (long long i = 0; i < n; i++)

#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)

#define ll long long

#define deb(x) cout << #x << "=" << x << endl

#define pb push_back

#define mp make_pair

#define F first

#define S second

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

#define clr(x) memset(x, false, sizeof(x))

#define sortall(x) sort(all(x))

#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)

#define PI 3.1415926535897932384626

#define mod 1000000007

typedef pair<long long, long long> pii;

typedef pair<ll, ll> pl;

typedef vector<long long> vi;

typedef vector<ll> vl;

typedef vector<pii> vpii;

typedef vector<pl> vpl;

typedef vector<vi> vvi;

typedef vector<vl> vvl;

int main()

{

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int t;

    cin >> t;

    while (t--)

    {

        //write code here

        string s;

        cin>>s;

        int n=s.length();

        vector<int>digit(n);

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

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

        }

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

            int j=i;

            while(j>=1 && digit[j]>0 && digit[j]>digit[j-1]+1 ){

                  swap(digit[j],digit[j-1]);

                  digit[j-1]--;

                  if(j>1) j--;

                  else break;

            }

        }

        string ans="";

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

            ans+=to_string(digit[i]);

        }

        cout<<ans<<endl;

    }

}

#include <bits/stdc++.h>

using namespace std;

#define gc getchar_unlocked

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

#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)

#define ll long long

#define deb(x) cout << #x << "=" << x << endl

#define pb push_back

#define mp make_pair

#define F first

#define S second

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

#define clr(x) memset(x, false, sizeof(x))

#define sortall(x) sort(all(x))

#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)

#define PI 3.1415926535897932384626

#define mod 1000000007

typedef pair<int, int> pii;

typedef pair<ll, ll> pl;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef vector<pii> vpii;

typedef vector<pl> vpl;

typedef vector<vi> vvi;

typedef vector<vl> vvl;

int find(vi &arr,int maxi){

      int n=arr.size();

      for(int i=n-1;i>=1;i--){

            if(arr[i]==maxi){

                  return i;

            }

      }

      return -1;

}

int main()

{

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int t;

    cin >> t;

    while (t--)

    {

        //write code here

        string s;

        string ans;

        cin>>s;

        int n=s.length();

        vi arr(n);

        fo(i,n){

        }

        fo(i,n-1){

            int maxi=*max_element(arr.begin()+1,arr.end());

            // cout<<maxi<<endl;

            if(maxi==0){

                  continue;

            }

            int index=find(arr,maxi);

            maxi--;

            arr[index]=maxi;

            swap(arr[index-1],arr[index]);

        }

        arr[n-1]-=1;

        swap(arr[n-2],arr[n-1]);

        fo(i,n){

            ans+=to_string(arr[i]);

        }

        cout<<ans<<endl;

    }

    return 0;

}

Answer: 1


#include <iostream>

#include <string>

int main() {

std::ios_base::sync_with_stdio(false);

std::cin.tie(nullptr);

int t;

std::cin >> t;

while (t--) {

std::string s;

std::cin >> s;

bool improved = true;

while (improved) {

improved = false;

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

if (s[i] == '0') continue;

for (int j = i-1; j >= 0; j--) {

std::string temp = s;

temp[i]--;

std::swap(temp[i], temp[j]);

if (temp > s) {

s = temp;

improved = true;

goto found_improvement;

}

}

}

found_improvement:;

}

std::cout << s << "\n";

}

return 0;

}

#include<bits/stdc++.h>

using namespace std;

#define fastio ios::sync_with_stdio(0);cin.tie(0)

int main()

{

fastio;

long long t,n,i,j;

string s;

char cur;

cin>>t;

while(t--)

{

cin>>s;

n=s.size();

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

{

cur=s[i];

for(j=i-1;j>=0;j--)

{

if(cur<=s[j]+1)

break;

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

s[j]=(char)(cur-1);

cur--;

if(cur=='0')

break;

}

}

cout<<s<<"\n";

}

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cerr.rdbuf(cout.rdbuf());
#define f(i,a,b) for (ll (i)=(a); (i)<(b); (i)++)
#define rf(i,a,b) for (ll (i)=(a); (i)>=(b); (i)--)
#define vll vector<ll>
#define vvll vector<vll>
#define in(n) ll n; cin>>n;
#define sin(s) string (s); cin>>(s);
#define vin(v,n) vll (v)(n); f(i,0,n)cin>>(v)[i];
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sort(v) sort(all(v));
#define rsort(v) sort(v) reverse(all(v));
#define pYES cout<<"YES\n";
#define pNO cout<<"NO\n";
const ll mod = 1000000007;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " = "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

template <typename T>
void _print(T x) { cerr << x; }
template <typename T, typename V>
void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template <typename T>
void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T>
void _print(set<T> s) { cerr << "{ "; for (T i : s) { _print(i); cerr << " "; } cerr << "}"; }
template <typename T, typename V>
void _print(map<T, V> m) { cerr << "{ "; for (auto i : m) { _print(i); cerr << " "; } cerr << "}"; }
/* *********************Template ends here************** */

void solve() {
    sin(s)
    ll n=s.length();
    f(i,0,n){
        // now check from i+1 to i+10
        ll bst=s[i]-'0';
        ll bstidx=i;
        f(j,i+1,min(i+10,n)){
            if (s[j]-'0'-(j-i)>bst){
                bst=s[j]-'0'-(j-i);
                bstidx=j;
            }
        }
        while(bstidx>i){
            s[bstidx]--;
            swap(s[bstidx],s[bstidx-1]);
            bstidx--;
        }
    }
    cout<<s<<endl;
}

int main() {
    fast;
    int t = 1;
    cin >> t;
    while(t--) solve();
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cerr.rdbuf(cout.rdbuf());
#define f(i,a,b) for (ll (i)=(a); (i)<(b); (i)++)
#define rf(i,a,b) for (ll (i)=(a); (i)>=(b); (i)--)
#define vll vector<ll>
#define vvll vector<vll>
#define in(n) ll n; cin>>n;
#define sin(s) string (s); cin>>(s);
#define vin(v,n) vll (v)(n); f(i,0,n)cin>>(v)[i];
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define sort(v) sort(all(v));
#define rsort(v) sort(v) reverse(all(v));
#define pYES cout<<"YES\n";
#define pNO cout<<"NO\n";
const ll mod = 1000000007;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " = "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

template <typename T>
void _print(T x) { cerr << x; }
template <typename T, typename V>
void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template <typename T>
void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T>
void _print(set<T> s) { cerr << "{ "; for (T i : s) { _print(i); cerr << " "; } cerr << "}"; }
template <typename T, typename V>
void _print(map<T, V> m) { cerr << "{ "; for (auto i : m) { _print(i); cerr << " "; } cerr << "}"; }
/* *********************Template ends here************** */

void solve() {
    sin(s)
    ll n=s.length();
    f(k,0,10){
        rf(i,n-1,1){
            ll l=i-1;
            ll r=i;
            while(l>=0 && s[r]-s[l]>1){
                s[r]--;
                swap(s[l],s[r]);
                l--; r--;
            }

        }
    }
    cout<<s<<endl;
}

int main() {
    fast;
    int t = 1;
    cin >> t;
    while(t--) solve();
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define INF 100000000
#define ll long long int

int main(){
    int t;					cin>>t;
    while(t--){
    	string s;
    	cin>>s;
    	int n=s.size();
    	vector<int>a;
    	for(int i=0;i<n;i++){
    		a.push_back(s[i]-'0');
    	}

    	for(int i=0;i<n;i++){
            int indx=i;
            int mx= a[i];
            for(int j=i;j<=n-1;j++){
                if(a[j]-(j-i)>=0 and a[j]-(j-i)>mx){
                    mx= a[j]-(j-i);
                    indx=j;
                }
            }
            while(indx>i){
                a[indx]--;
                swap(a[indx],a[indx-1]);
                indx--;
            }
    	}
    	for(int i=0;i<n;i++){
            cout<< a[i];
    	}
    	cout<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define INF 100000000
#define ll long long int

int main(){
    int t;					cin>>t;
    while(t--){
    	string s;
    	cin>>s;
    	int n=s.size();
    	vector<int>a;
    	for(int i=0;i<n;i++){
    		a.push_back(s[i]-'0');
    	}

    	for(int i=0;i<n;i++){
            int indx=i;
            int mx= a[i];
            for(int j=i;j<=min(n-1, i+12);j++){
                if(a[j]-(j-i)>=0 and a[j]-(j-i)>mx){
                    mx= a[j]-(j-i);
                    indx=j;
                }
            }
            while(indx>i){
                a[indx]--;
                swap(a[indx],a[indx-1]);
                indx--;
            }
    	}
    	for(int i=0;i<n;i++){
            cout<< a[i];
    	}
    	cout<<endl;
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
void solve (int n){
	string s=to_string(n);
	int m=s.size();
	for(int j=0; j<m; j++){
	for(int i=1; i<m; i++){
		if(s[i]-1>0 && s[i-1]<s[i]-1){
		    int k=s[i];
		    s[i]=s[i-1];
		    s[i-1]=k-1;
		}

	}
	}

	cout<<s;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		solve(n);
		cout<<endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
void solve (string s){

	int m=s.size();
	bool flag =  true;
	while(flag){
		flag =false;
		for(int i=1; i<m; i++){

		if(s[i]-1>0 && s[i-1]<s[i]-1){
		    int k=s[i];
		    s[i]=s[i-1];
		    s[i-1]=k-1;
		    flag=true;
		}

	}
	}

	cout<<s;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		solve(s);
		cout<<endl;
	}
}

Answer: 2