/* In the Name of ALLAH */
/** Minhaz Uddin
   Dept of CSE,Comilla University*/

#include<bits/stdc++.h>
#define ll long long
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define tst int t;cin>>t;while(t--)
#define pb push_back
#define rvs reverse(v.begin(),v.end())
#define srt sort(v.begin(),v.end())

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const ll  N = 1e7+9;

void solve()
{
string s;
cin>>s;
ll n,i,j, sum = 0, c2 = 0, c3 = 0;
for(i = 0;i<s.size();i++)
{
    sum+=s[i]-'0';
    if(s[i]=='2')c2++;
    else if(s[i]=='3')c3++;
}
if(sum%9==0)yes
else
{
    ll mod =9- (sum%9);
    if(mod%2==1)mod+=9;
    while(mod-6>=0 && c3>0)
    {
        mod-=6;
        c3--;
    }
    while(mod-2>=0 && c2>0)
    {
        mod-=2;
        c2--;
    }
    if(mod==0)yes
    else no
}
}

int main()
{
optimize();
tst{
solve();
}
return 0;
}

/* In the Name of ALLAH */
/** Minhaz Uddin
   Dept of CSE,Comilla University*/

#include<bits/stdc++.h>
#define ll long long
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define tst int t;cin>>t;while(t--)
#define pb push_back
#define rvs reverse(v.begin(),v.end())
#define srt sort(v.begin(),v.end())

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const ll  N = 1e7+9;

void solve()
{
string s;
cin>>s;
ll n,i,j, sum = 0, c2 = 0, c3 = 0;
for(i = 0;i<s.size();i++)
{
    sum+=s[i]-'0';
    if(s[i]=='2')c2++;
    else if(s[i]=='3')c3++;
}
if(sum%9==0)yes
else
{
    ll mod =9- (sum%9);
    //cout<<mod<<endl;
    if(mod==8)
    {
        if(c3>=1 && c2>=1)yes
        else if(c3>=0 && c2>=4)yes
        else no
    }
    else if(mod==7)
    {
        if(c2>=2 && c3>=2)yes
        else if(c3>=1 && c2>=5)yes
        else no
    }
    else if(mod==6)
    {
       if(c3>=1)yes
       else if(c2>=3) yes
        else no
    }
    else if(mod==5)
    {
        if(c3>=2 && c2>=1)yes
        else if(c3>=1 && c2>=4)yes
        else no
    }
    else if(mod==4)
    {
        if(c2>=2 )yes
        else no
    }
    else if(mod==3)
    {
        if(c3>=1 && c2>=3)yes
        else if(c3>=2)yes
        else if(c2>=6)yes
        else no
    }
    else if(mod==2)
    {
        if(c2>=1)yes
        else no
    }
    else if(mod==1)
    {
        if(c2>=2 && c3>=1)yes
        else if(c2>=5)yes
        else no
    }

    else no
}
}

int main()
{
optimize();
tst{
solve();
}
return 0;
}

Answer: 1


// Problem: B. Transfusion

// Contest: Codeforces - Codeforces Round 991 (Div. 3)

// URL: https://codeforces.com/contest/2050/problem/B

// Memory Limit: 256 MB

// Time Limit: 2000 ms

//

// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("O3")

//#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define Begin ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

#define pii pair<int, int>

#define pdd pair<double, double>

#define lowbit(x) (x & -x)

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

#define Rep(i, x) for(auto &i : x)

#define L(i, x, y) for(int i = x; i <= y; ++ i)

#define R(i, x, y) for(int i = x; i >= y; -- i)

#define rd(x) scanf("%lld", &(x))

#define p_b push_back

#define ft first

#define sd second

using namespace std;

template <class T> using vc = vector<T>;

template <class T> using _pq = priority_queue<T, vector<T>, greater<T>>;

using i64 = long long;

using u64 = unsigned long long;

using u32 = unsigned;

const int N = 2e5 + 10;

void solve() {

	int n; cin >> n;

	vc<i64> a(n);

	L(i, 0, n - 1) cin >> a[i];

	i64 sum = reduce(a.begin(), a.end());

	if(sum % n) {

		cout << "NO\n";

		return;

	}

	i64 sd = sum / n;

	//cout << sd << "\n";

	L(i, 0, n - 1) {

		//cout << i << " " << a[i] << "\n";

		if(i >= n - 2 && a[i] != sd) {

			cout << "NO\n";

			return;

		}

		if(a[i] == sd) continue;

		else {

				if(a[i] < sd) {

					i64 dif = sd - a[i];

					a[i] = sd;

					if(a[i + 2] >= dif) a[i + 2] -= dif;

					else {

						cout << "NO\n";

						return;

					}

				}

				else {

					i64 dif = a[i] - sd;

					a[i] = sd;

					a[i + 2] += dif;

				}

		}

	}

	L(i, 0, n - 1) {

		if(a[i] != sd) {

			cout << "NO\n";

			return;

		}

	}

	cout << "YES\n";

}

int main() {

	Begin;

	int _; _ = 1;

	cin >> _;

	while(_--) solve();

	return 0;

}

// Problem: C. Uninteresting Number

// Contest: Codeforces - Codeforces Round 991 (Div. 3)

// URL: https://codeforces.com/contest/2050/problem/C

// Memory Limit: 256 MB

// Time Limit: 2000 ms

//

// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("O3")

//#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define Begin ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

#define pii pair<int, int>

#define pdd pair<double, double>

#define lowbit(x) (x & -x)

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

#define Rep(i, x) for(auto &i : x)

#define L(i, x, y) for(int i = x; i <= y; ++ i)

#define R(i, x, y) for(int i = x; i >= y; -- i)

#define rd(x) scanf("%lld", &(x))

#define p_b push_back

#define ft first

#define sd second

using namespace std;

template <class T> using vc = vector<T>;

template <class T> using _pq = priority_queue<T, vector<T>, greater<T>>;

using i64 = long long;

using u64 = unsigned long long;

using u32 = unsigned;

const int N = 2e5 + 10;

void solve() {

	string s; cin >> s;

	int sum = 0; map<int, int> mp;

	int n = s.size();

	L(i, 0, n - 1) {

		if(s[i] - '0' == 2) mp[2] ++;

		if(s[i] - '0' == 3) mp[3] ++;

		sum += s[i] - '0';

	}

	if(sum % 9 == 0) {

		cout << "YES\n";

		return;

	}

	int dif = sum % 9;

	L(i, 0, mp[2]) {

		L(j, 0, mp[3]) {

			if((i * 2 + j * 6 + dif) % 9 == 0) {

				cout << "YES\n";

				return;

			}

		}

	}

	cout << "NO\n";

}

int main() {

	Begin;

	int _; _ = 1;

	cin >> _;

	while(_--) solve();

	return 0;

}

Answer: 2


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

int32_t main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int sum = 0;
        for (char c : s)
        {
            sum += c - '0';
        }
        int twos = 0;
        int threes = 0;
        for (char c : s)
        {
            if (c == '2')
            {
                twos++;
            }
            else if (c == '3')
            {
                threes++;
            }
        }
        int flag = 0;
        for (int i = 0; i <= min(10ll, twos); i++)
        {
            for (int j = 0; j <= min(10ll, threes); j++)
            {
                if ((sum + 2 * i + 6 * j) % 9 == 0)
                {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag)
        {
            cout << "YES\n";
        }
        else
        {
            cout << "NO\n";
        }
    }
    return 0;
}

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

int32_t main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int sum = 0;
        string s = to_string(n);
        for (char c : s)
        {
            sum += c - '0';
        }
        if (sum % 9 == 0)
        {
            cout << "YES\n";
            continue;
        }
        int twos = 0;
        int threes = 0;
        for (char c : s)
        {
            if (c == '2')
            {
                twos++;
            }
            else if (c == '3')
            {
                threes++;
            }
        }
        int flag = 0;
        for (int i = 0; i <= twos; i++)
        {
            for (int j = 0; j <= threes; j++)
            {
                if ((sum + 2 * i + 6 * j) % 9 == 0)
                {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag)
        {
            cout << "YES\n";
        }
        else
        {
            cout << "NO\n";
        }
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

#define int long long

using namespace std;

void solve(){

    string s;

    cin >> s;

    int cnt = 0, cnt2 = 0, sum = 0;

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

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

        if (c == 2){

            cnt++;

        }

        else if (c == 3){

            cnt2++;

        }

        sum += c;

    }

    int x = sum % 9;

    if (x == 0){

        cout << "YES\n";

        return;

    }

    while (cnt > 0){

        x += 2;

        if (x % 9 == 0){

            cout << "YES\n";

            return;

        }

        cnt--;

    }

    int x = sum % 9;

    if (x == 0){

        cout << "YES\n";

        return;

    }

    while (cnt2 > 0){

        x += 6;

        if (x % 9 == 0){

            cout << "YES\n";

            return;

        }

        cnt2--;

    }

    int x = sum % 9;

    if (x == 0){

        cout << "YES\n";

        return;

    }

    cout << "NO\n";

}

int32_t main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int t;

    cin >> t;

    while (t--){

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>

#define int long long

using namespace std;

void solve(){

    string s;

    cin >> s;

    int cnt = 0, cnt2 = 0, sum = 0;

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

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

        if (c == 2){

            cnt++;

        }

        else if (c == 3){

            cnt2++;

        }

        sum += c;

    }

    int x = sum % 9;

    for (int i = 0; i <= min(8ll, cnt); i++){

        for (int j = 0; j <= min(8ll, cnt2); j++){

            if ((2 * i + 6 * j + x) % 9 == 0){

                cout << "YES\n";

                return;

            }

        }

    }

    cout << "NO\n";

}

int32_t main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    int t;

    cin >> t;

    while (t--){

        solve();

    }

    return 0;

}

Answer: 2


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

    #define ll long long
    #define ld long double
    #define pii pair<int, int>
    #define mp make_pair
    #define pb push_back
    #define eb emplace_back
    #define fi first
    #define se second

    const int INF = 1e9;
    const ll INFLL = 1e18;
    const int MOD = 1e9 + 7;

    char rect(vector<char> &s){
        int n=s.size();

     char max=s[0];
     int ind=0;
        for(int i=1; i<min(n,10); i++){
          if(s[i]-i>max){
          max=s[i]-i;
          ind=i;
        }}
        s.erase(s.begin()+ind);
        return max;

    }
    int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        // Declare any additional variables here

        int t; // number of test cases
        cin >> t;

        while (t--) {
          string s;
          cin >> s;
          ll n;
          n=s.size();
          vector<char> v;
          if(n>9){
          for(int i=0; i<10;i++)
           v.pb(s[i]);
           cout<<rect(v);
           for(int i=10;i<n;i++){
            v.pb(s[i]);
           cout<<rect(v);}
           for(int i=0;i<9;i++){
               cout<<rect(v);
           }

          }
          else{
        for(int i=0;i<n;i++){
        v.pb(s[i]);
        }
        int size=v.size();
        for(int i=0;i<size;i++)
        cout<<rect(v);
          }
        cout<<"\n";

        }

        return 0;
    }

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

#define ll long long
#define ld long double
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

const int INF = 1e9;
const ll INFLL = 1e18;
const int MOD = 1e9 + 7;
    bool customSort(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.first == b.first) {
        return a.second > b.second;
    }
    return a.first < b.first; }
char rect(vector<char> &s){
    int n=s.size();
    vector<pair<char,ll>> v(n);
    vector<char> vis;

    for(int i=0; i<min(n,10); i++){
      if(s[i]!='\0'){
        v[i].fi=s[i]-i;
        v[i].se=i;}

    }
sort(v.begin(),v.end(),customSort);
    s.erase(s.begin()+v[v.size()-1].se);
    return v[v.size()-1].fi;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Declare any additional variables here

    int t; // number of test cases
    cin >> t;

    while (t--) {
      string s;
      cin >> s;
      ll n;
      n=s.size();
     vector<char> v;
     for(int i=0;i<n;i++){
        v.pb(s[i]);
     }
     for(int i=0;i<n;i++)
      cout<<rect(v);

    }

    return 0;
}

Answer: 1


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

void solve(string s) {
    long long n = s.size();
    bool updated = true;

    while (updated) {
        updated = false;
        for (long long i = n - 1; i > 0; --i) {
            if (s[i] > s[i - 1] + 1) {
                s[i]--;
                swap(s[i], s[i - 1]);
                updated = true;  // Mark as updated to repeat the process
            }
        }
    }

    cout << s;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        solve(s);
        cout << endl;
    }
    return 0;
}

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

int main(){
int t;
cin>>t;
while(t--){
    string n;
    cin>>n;
    int change(1);
    while(change!=0){
        change=0;
        for(int i=1;i<n.length();i++){
            if((n[i-1] - '0')<((n[i] - '0')-1)){
                int temp=n[i-1] - '0';
                n[i-1]=char((n[i] - '0') - 1) + '0';
                n[i]=char(temp) + '0';
                change++;
            }
        }
    }
    cout<<n<<endl;
}
return 0;
}

Answer: 2


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve() {
    int t;
    cin >> t;

    while (t--) {
        string s;
        cin >> s;
        int n = s.size();

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

        }
        cout<<s<<endl;

}}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve() {
    int t;
    cin >> t;

    while (t--) {
        string s;
        cin >> s;
        int n = s.size();

        bool changed;
        do {
            changed = false;
            for (int i = 1; i < n; i++) {
                if (s[i] > s[i-1] && s[i] != '0' && s[i] != s[i-1] + 1) {
                    s[i] -= 1;
                    swap(s[i], s[i-1]);
                    changed = true;
                }
            }
        } while (changed);

        cout<<s<<endl;

}}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

Answer: 2


#include "bits/stdc++.h"

#define int long long

#define fi first

#define se second

#define vi vector<int>

#define vii vector<pair<int,int>>

#define vvi vector<vector<int>>

#define vch vector<char>

#define vb vector<bool>

#define vst vector<string>

#define pi pair<int,int>

#define eb emplace_back

#define pb push_back

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

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

#define input(v,n) for(int i=0;i<n;i++) cin>>v[i];

#define print(v) for(auto &x:v) cout<<x<<" "; cout<<endl;

#define total(a) accumulate(all(a),0LL)

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

#define re(i,n) for(decltype(n) i=n-1;i>=0;--i)

#define fo1(i,a,b) for(decltype(b) i=a;i<b;++i)

#define re1(i,a,b) for(decltype(a) i=a;i>=b;--i)

#define w(t) int t; cin>>t; while(t--)

#define debug(x) cerr << #x << " = " << x << endl

#define debugv(x) for(auto &i:x) cerr << i << " "; cerr << endl

#define mini(v) *min_element(all(v))

#define maxi(v) *max_element(all(v))

#define popcount(x) __builtin_popcountll(x)

#define bit(x) (1LL << (x))

#define lowbit(x) (x & -x)

#define checkbit(x, i) ((x >> i) & 1)

#define yesno(x) if(x) cout<<"YES"<<endl; else cout<<"NO"<<endl;

#define yes cout<<"YES"<<endl;

#define no cout<<"NO"<<endl;

#define mod 1000000007

#define endl '\n'

using namespace std;

void fastio() {

ios::sync_with_stdio(false);

cin.tie(NULL); cout.tie(0); cout.precision(10);

srand(chrono::high_resolution_clock::now().time_since_epoch().count());

}

void solve() {

string s ; cin >> s ;

int n = s.size() ;

fo(i,n){

int minn = min(i+10,n);

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

fo1(j,i+1,minn){

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

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

ind = j;

}

}

while(ind > i){

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

ind--;

}

s[i] = curr + '0';

}

cout << s << endl;

}

int32_t main() {

fastio();

w(t) solve();

return 0;

}

#include "bits/stdc++.h"

#define int long long

#define fi first

#define se second

#define vi vector<int>

#define vii vector<pair<int,int>>

#define vvi vector<vector<int>>

#define vch vector<char>

#define vb vector<bool>

#define vst vector<string>

#define pi pair<int,int>

#define eb emplace_back

#define pb push_back

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

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

#define input(v,n) for(int i=0;i<n;i++) cin>>v[i];

#define print(v) for(auto &x:v) cout<<x<<" "; cout<<endl;

#define total(a) accumulate(all(a),0LL)

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

#define re(i,n) for(decltype(n) i=n-1;i>=0;--i)

#define fo1(i,a,b) for(decltype(b) i=a;i<b;++i)

#define re1(i,a,b) for(decltype(a) i=a;i>=b;--i)

#define w(t) int t; cin>>t; while(t--)

#define debug(x) cerr << #x << " = " << x << endl

#define debugv(x) for(auto &i:x) cerr << i << " "; cerr << endl

#define mini(v) *min_element(all(v))

#define maxi(v) *max_element(all(v))

#define popcount(x) __builtin_popcountll(x)

#define bit(x) (1LL << (x))

#define lowbit(x) (x & -x)

#define checkbit(x, i) ((x >> i) & 1)

#define yesno(x) if(x) cout<<"YES"<<endl; else cout<<"NO"<<endl;

#define yes cout<<"YES"<<endl;

#define no cout<<"NO"<<endl;

#define mod 1000000007

#define endl '\n'

using namespace std;

void fastio() {

ios::sync_with_stdio(false);

cin.tie(NULL); cout.tie(0); cout.precision(10);

srand(chrono::high_resolution_clock::now().time_since_epoch().count());

}

void solve() {

string s ; cin >> s ;

int n = s.size() ;

fo(i,n){

int minn = min(i+10,n);

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

fo1(j,i+1,minn){

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

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

ind = j;

}

}

while(ind > i){

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

ind--;

}

}

cout << s << endl;

}

int32_t main() {

fastio();

w(t) solve();

return 0;

}

Answer: 1


for _ in range(int(input())):
  s=list(map(int,input()[::-1]))
  a=[]
  while s:
    m=s[-1]
    x=-1
    for i in range(-1,max(-11,-len(s)-1),-1):
      if x-m==9:
        break
      if s[i]+i+1>m:
        m=s[i]+i+1
        x=i
    a.append(m)
    del s[x]
  print("".join(map(str,a)))

for _ in range(int(input())):
  s=list(map(int,input()))
  a=[]
  while s:
    m=s[0]
    x=0
    for i in range(1,min(10,len(s))):
      if x+m==9:
        break
      if s[i]-i>m:
        m=s[i]-i
        x=i
    a.append(m)
    del s[x]
  print("".join(map(str,a)))

Answer: 1


t =  int(input())

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

s.insert(j, s[i] - (i-j))

s.pop(i+1)

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()

t =  int(input())

def solve():

s = [int(i) for i in list(input())]

n = len(s)

for i in range(1,n,1):

for j in range(max(i-10, 0), i):

if s[i] - (i-j) > s[j]:

tmp = s[j:i]

s[j] = s[i] - (i-j)

s[j+1:i+1] = tmp

break

print("".join([str(i) for i in s]))

return

for i in range(t):

solve()

Answer: 2


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

// typedef vector<int> vi;
// typedef vector<vector<int>> vvi;
// typedef vector<long long> vl;
// typedef vector<vector<long long>> vvl;
// typedef vector<bool> vb;
// typedef vector<vector<bool>> vvb;
// typedef map<int, int> mii;
// typedef map<long long, long long> mll;
// typedef pair<int, int> pii;
// typedef pair<long long, long long> pll;
// typedef unsigned long long ull;
// typedef long double ld;
// typedef long long ll;
typedef long long ll;
#define f(i,a,b) for(int i = a; i < b ; i++)
#define geti(n) int n; cin >> n;
#define getll(n) long long int n; cin >> n;
#define gets(s) string s; cin >>s;
#define getv(v,n) vector<int> v(n) ; f(i, 0 ,n) cin >> v[i];
#define srt(v) sort(v.begin(), v.end());
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
    string s;
    cin>>s;
    vector<long long> v(s.size());
    long long n = s.size();
    for(long long i = 0; i<s.size(); i++){
        v[i] = (s[i] - '0');
    }
    for(long long i = 0; i<n; i++){
        long long max = v[i];
        long long ind = i;
        for(long long j = i; j<min(n, j+9); j++){
            long long mx = v[j] - (j - i);
            if(mx > max){
                max = mx;
                ind = j;
            }
        }
        for(long long k = ind; k>i; k--){
            v[k] = v[k-1];
        }
        v[i] = max;
        cout<<max;
    }
    cout<<endl;
}

int32_t main()
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    for(int i = 0; i < t; i++)
    {
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured:"  << elapsed.count() * 1e-9 << ",seconds.";
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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() {
    int TESTS = 1; cin >> TESTS;
    while (TESTS --> 0) {
        solve();
        cout << '\n';
    }
    return 0;
}

Answer: 2


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

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        cout << "\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b - 1) * 10 + a;
    int num2 = (a * 10) + b;
    return num1 > num2;
}

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

    int n = num.length();

    if (n == 1)
    {
        cout << num;
        return;
    }

    string ans = "";
    ans += num[n - 1];

    for (int i = n - 2; i >= 0; i--)
    {
        ans = num[i] + ans;
        if (ans[0] < ans[1])
        {
            for (int j = 0; j < ans.length() - 1; j++)
            {
                if (ans[j] < ans[j + 1])
                {
                    if (swapOrNot(ans[j] - '0', ans[j + 1] - '0'))
                    {
                        char modifiedChar = ans[j + 1] - 1;
                        swap(ans[j], modifiedChar);
                        ans[j + 1] = modifiedChar;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }

    cout << ans;
}

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

int main()
{
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
        cout<<"\n";
    }
}

bool swapOrNot(int a, int b)
{
    int num1 = (b-1)*10+a;
    int num2 = (a*10)+b;
    return num1>num2;
}

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

    int n=num.length();

    if(n==1)
    {
        cout<<num;
        return;
    }

    string ans="";
    ans+=num[n-1];

    for(int i=n-2;i>=0;i--)
    {
        ans=num[i]+ans;
        for(int j=0;j<ans.length()-1;j++)
        {
           if(swapOrNot(ans[j]-'0',ans[j+1]-'0'))
           {
              char modifiedChar = ans[j+1] - 1;
              swap(ans[j], modifiedChar);
              ans[j+1] = modifiedChar;
           }
        }
    }

    cout<<ans;
}

Answer: 1


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

using ll = long long;
void setIO() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

void solve() {
	string s; cin >> s;
	string ans = "";
	int n = s.length();
	vector<int> arr;
	vector<int> diffs(n, 0);
	for(int i = 0; i < n; i++) {
		arr.push_back(s[i] - '0');
	}

	int amtskipped = 0;
	for(int i = 0; i < n; i++) {
		int bestIndex = 0;
		int bestAmt = 0;
		for(int j = 0; j < min((int) arr.size(), 9); j++) {
			if(arr[j] - j > bestAmt) {
				bestIndex = j;
				bestAmt = arr[j] - j;
			}
		}

		arr.erase(arr.begin() + bestIndex);
		ans += to_string(bestAmt);
	}

	cout << ans << "\n";
}

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

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

using ll = long long;
void setIO() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

void solve() {
	string s; cin >> s;
	string ans = "";
	int n = s.length();

	set<int> getRid;
	for(int i = 0; i < n; i++) {
		int bestIndex = i;
		int bestAmt = 0;
		for(int j = i; j < min((int) s.size(), i+10); j++) {
			if(s[j] - '0' - (j-i) > bestAmt) {
				bestIndex = j;
				bestAmt = s[j] - '0' - (j-i);
			}
		}

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

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

	}

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

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

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(i+10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

Answer: 2


#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define pb push_back
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define f first
#define s second

using namespace std;

void solve()
{
    string s;

    cin >> s;

    for (int i = 0; i < s.size(); i++)
    {
        int q = 0;
        int mx = s[i] - 48;
        for (int j = 1; i + j < s.size() && j < 10; j++)
        {
            int x = s[i + j] - 48;
            x -= j;
            if (x > mx)
            {
                q = j;
                mx = x;
            }
        }

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

        s[i] -= q;
    }

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

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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

    return 0;
}

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define pb push_back
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define f first
#define s second

using namespace std;

void solve()
{
    string s;

    cin >> s;

    for (int i = 0; i < s.size(); i++)
    {
        int q = 0;
        int mx = s[i] - 48;
        for (int j = 1; i + j < s.size() && j < i + 10; j++)
        {
            int x = s[i + j] - 48;
            x -= j;
            if (x > mx)
            {
                q = j;
                mx = x;
            }
        }

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

        s[i] -= q;
    }

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

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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

    return 0;
}

Answer: 1


def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 2:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            for jup in pos:
                if x[jup[0]-1] < max_n - 1:
                    x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                    check_q = 1

            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x

t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)

def highest_var(x):
    while True:
        max_n = max(x)
        check = False
        while max_n > 1:
            for i in range(1, len(x)):
                if x[i] == max_n and x[i - 1] < max_n - 1:
                    x[i - 1], x[i] = x[i] - 1, x[i - 1]
                    check = True
            max_n -= 1
        if not check:
            break
    return x

t = int(input())
ab_res = []
for _ in range(t):
    s = input().strip()
    x = list(map(int, s))
    x = highest_var(x)
    ab_res.append(''.join(map(str, x)))

print('\n'.join(ab_res))

Answer: 2


from sys import stdin
from typing import Any
import math
input = stdin.readline
def ri(): return int(input())
def rl(v_type=int,output=list): return output(map(v_type, input().split()))
def rs(): return input().rstrip()
def out(x, sep='\n'): print(sep.join(x))

def solve():
    s = rs()
    n = len(s)
    a = list(map(int,s))
    for i in range(n-2,-1,-1):
        for j in range(i+1, min(i + 10,n)):
            if a[j-1] + (1) < a[j]:
                a[j-1],a[j] = a[j] - (1),a[j-1]
            else:
                break

    return "".join(map(str,a))

output = []
for _ in range(ri()):
    output.append(str(solve()))
out(output)

from sys import stdin
from typing import Any
import math
input = stdin.readline
def ri(): return int(input())
def rl(v_type=int,output=list): return output(map(v_type, input().split()))
def rs(): return input().rstrip()
def out(x, sep='\n'): print(sep.join(x))

def solve():
    s = rs()
    n = len(s)
    a = list(map(int,s))
    for i in range(1,n):
        for j in range(i -1,max(-1,i - 11),-1):
            if a[j] + 1 < a[j + 1]:
                a[j],a[j+1] = a[j+1] - 1, a[j]
            else:
                break

    return "".join(map(str,a))

output = []
for _ in range(ri()):
    output.append(str(solve()))
out(output)

Answer: 2


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int INF = 2e9;
void solve() {
    string s;
    cin >> s;
    vector<int> a;
    for(auto c : s) {
        a.push_back(c - '0');
    }

    vector<int> b;
    for(int i = 0;i < a.size(); ++i) {
        int index = -1,maxx = -INF;
        for(int j = i;j < min(i + 10, (int)a.size());++j) {
            int tmp = a[j] - (j - i);
            if(tmp > maxx) {
                maxx = tmp;
                index = j;
            }
        }
            b.push_back(maxx);

            while(index > i) {
                swap(a[index], a[index - 1]);
                index --;
            }
        }
    for(auto x : b) cout << x;
    cout << endl;
}

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

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int INF = 2e9;
void solve() {
    string s;
    cin >> s;
    vector<int> a;
    for(auto c : s) {
        a.push_back(c - '0');
    }

    vector<int> b;
    for(int i = 0;i < a.size(); ++i) {
        int index = -1,maxx = -INF;
        for(int j = i;j < a.size();++j) {
            int tmp = a[j] - (j - i);
            if(tmp > maxx) {
                maxx = tmp;
                index = j;
            }
        }
            b.push_back(maxx);

            while(index > i) {
                swap(a[index], a[index - 1]);
                index --;
            }
        }
    for(auto x : b) cout << x;
    cout << endl;
}

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

Answer: 1


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

#define int long long
#define nl cout<<endl
#define vec vector<int>
#define vecs vector<string>
#define ddv vector<vec>
#define ub upper_bound
#define lb lower_bound
#define prnt(v) for(auto i:v) cout<<i<<" "; nl
#define inp(v); for(auto &i:v) cin>>i;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

const int N = 2e5;
const int M = 1e9+7;

int factorials[N+1];
int factInv[N+1];
bool isPrime[N+1];

int gcd(int a,int b){ //O(logb)
    if(!b) return a;
    return gcd(b,a%b);
}

int lcm(int a,int b){ //O(logb)
    return (a*b/gcd(a,b)) % M;
}

int binExp(long long a, int b) { //O(logb)
    a %= M;
    int ans = 1;
    while (b) {
        if (b & 1) ans = (ans * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return ans;
}

int inv(int n){ //O(logM)
    return binExp(n,M-2);
}

void FACT(){ //O(N)
    factorials[0] = 1;
    for (int i = 1; i <= N; i++) {
        factorials[i] = factorials[i - 1] * 1LL * i % M;
    }
}

void FACTINV(){ //O(N)
    for (int i = 0; i <= N; i++) {
        factInv[i] = inv(factorials[i]);
    }
}

void SIEVE(){ //O(NloglogN)

    //Pre-compute

    fill(isPrime, isPrime+N, true);
    isPrime[0]=isPrime[1]=false;

    for(int i = 2; i <= N; ++i){
        if(isPrime[i]){
            for(int j=2*i ; j<=N ; j+=i)
                isPrime[j]=false;
        }
    }
}

long long nCr(int n, int r){ //O(1) excl. Pre-Comp which is O(N).

    // Pre-Compute before using this func.
    // FACT();
    // FACTINV();

    if (r > n) return 0;
    return (factorials[n] * 1LL * factInv[r] % M * factInv[n - r] % M) % M;
}

void solve(){
    string s,ans="";
    cin>>s;
    int n = s.length();

    vector<bool> used(n,false);

    for(int i=0 ; i<n ; ){
        if(used[i]){
            ++i;
            continue;
        }

        int num = s[i]-'0';
        int numi = i;
        used[i] = true;
        ans.pb(s[i]);
        int bunk = 0;

        //
        // cout<<i<<endl;

        for(int j=i+1 ; j < min(i+10,n) ; ++j){
            if(used[j]){
                ++bunk;
                continue;
            }
            //
            // cout<<j<<endl;

            int trnum = s[j]-'0'-(j-i)+bunk;
            if(trnum > num){
                used[numi] = false;
                numi = j;
                num = trnum;
                ans.ppb();
                ans.pb(s[j]-(j-i)+bunk);
                used[numi] = true;
            }
        }

        //
        // cout<<ans<<endl;
        // prnt(used);

        if(numi == i) ++i;
    }

    cout<<ans<<endl;
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    // FACT();
    // FACTINV();
    // SIEVE();

    int t;
    cin>>t;

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

    return 0;
}

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

#define int long long
#define nl cout<<endl
#define vec vector<int>
#define vecs vector<string>
#define ddv vector<vec>
#define ub upper_bound
#define lb lower_bound
#define prnt(v) for(auto i:v) cout<<i<<" "; nl
#define inp(v); for(auto &i:v) cin>>i;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

const int N = 2e5;
const int M = 1e9+7;

int factorials[N+1];
int factInv[N+1];
bool isPrime[N+1];

int gcd(int a,int b){ //O(logb)
    if(!b) return a;
    return gcd(b,a%b);
}

int lcm(int a,int b){ //O(logb)
    return (a*b/gcd(a,b)) % M;
}

int binExp(long long a, int b) { //O(logb)
    a %= M;
    int ans = 1;
    while (b) {
        if (b & 1) ans = (ans * 1LL * a) % M;
        a = (a * 1LL * a) % M;
        b >>= 1;
    }
    return ans;
}

int inv(int n){ //O(logM)
    return binExp(n,M-2);
}

void FACT(){ //O(N)
    factorials[0] = 1;
    for (int i = 1; i <= N; i++) {
        factorials[i] = factorials[i - 1] * 1LL * i % M;
    }
}

void FACTINV(){ //O(N)
    for (int i = 0; i <= N; i++) {
        factInv[i] = inv(factorials[i]);
    }
}

void SIEVE(){ //O(NloglogN)

    //Pre-compute

    fill(isPrime, isPrime+N, true);
    isPrime[0]=isPrime[1]=false;

    for(int i = 2; i <= N; ++i){
        if(isPrime[i]){
            for(int j=2*i ; j<=N ; j+=i)
                isPrime[j]=false;
        }
    }
}

long long nCr(int n, int r){ //O(1) excl. Pre-Comp which is O(N).

    // Pre-Compute before using this func.
    // FACT();
    // FACTINV();

    if (r > n) return 0;
    return (factorials[n] * 1LL * factInv[r] % M * factInv[n - r] % M) % M;
}

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

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

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

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

        // cout<<s<<endl;
    }

    cout<<s<<endl;
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    // FACT();
    // FACTINV();
    // SIEVE();

    int t;
    cin>>t;

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

    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(x) cerr << #x << " = " << (x) << endl
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
//最大公约数
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T , vector<T> , greater<>>;

constexpr int mod = 998244353;
//快速幂
ll power(ll a, ll b) {
    a %= mod;
    ll res = 1;
    while (b > 0) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

void solve(){
	string s;
	cin>>s;
	int n = s.size();
	vector<int> st;
	vector<int> cur;
	for (int i=0;i<n;i++){
		int num = s[i]-'0';
		while (!st.empty() && num>st.back()+1){
			num--;
			cur.push_back(st.back());
			st.pop_back();
		}
		st.push_back(num);
		while (!cur.empty()){
			st.push_back(cur.back());
			cur.pop_back();
		}
	}
	for (auto v:st) cout<<v;
	cout<<"\n";
}

int main(){
    fastio;
    cout << fixed << setprecision(10);
    int T;
    cin>>T;

    while (T--){
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(x) cerr << #x << " = " << (x) << endl
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
//最大公约数
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T , vector<T> , greater<>>;

constexpr int mod = 998244353;
//快速幂
ll power(ll a, ll b) {
    a %= mod;
    ll res = 1;
    while (b > 0) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

void solve(){
	string s;
	cin>>s;
	int n = s.size();
	vector<array<int,2>> st;
	vector<array<int,2>> cur;
	for (int i=0;i<n;i++){
		int num = s[i]-'0';
		while (!st.empty() && num>st.back()[0]+st.back()[1]){
			cur.push_back(st.back());
			num -= st.back()[1];
			st.pop_back();
		}
		if (!st.empty() && st.back()[0]==num){
			st[st.size()-1][1]++;
		}
		else{
			st.push_back({num,1});
		}
		while (!cur.empty()){
			auto v = cur.back();
			cur.pop_back();
			st.push_back(v);
		}
	}
	for (auto v:st){
		for (int i=0;i<v[1];i++) cout<<v[0];
	}
	cout<<"\n";
}

int main(){
    fastio;
    cout << fixed << setprecision(10);
    int T;
    cin>>T;

    while (T--){
        solve();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

string canMakeDivisibleBy9(string s) {

    int n=s.length();

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

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

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

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

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

            }

        }

    }

    return s;

}

int main() {

    int t;

    cin >> t;

    while (t--) {

        string s;

        cin >> s;

        cout << (canMakeDivisibleBy9(s)) << endl;

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

string canMakeDivisibleBy9(string s) {

    int n=s.length();

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

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

        for(int j=i;j>0 && s[j]-s[j-1]>1;j--){

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

        }

    }

    return s;

}

int main() {

    int t;

    cin >> t;

    while (t--) {

        string s;

        cin >> s;

        cout << (canMakeDivisibleBy9(s)) << endl;

    }

    return 0;

}

Answer: 2


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

int main()
{
    int t;
    cin>>t;
    for(int m=0;m<t;m++)
    {

      string s;
      cin>>s;
      for(int i=0;i<s.size();i++)
      {
        int max_index=i;
        int d=s[i]-'0';
        for(int j=i+1;j<s.size()&&j<=i+9;j++)
        {
          int x=(s[j]-'0')-(j-i);
           if(x>d)
           {
             max_index=j;
             d=x;
           }
        }
        if(max_index!=i)
        { string p=s;

          for(int j=i+1;j<=max_index;j++)
          {
            p[j]=s[j-1];
          }
          p[i]=d+'0';
          s=p;

        }

      }

      cout<<s<<endl;

    }
    return 0;
}

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

int main()
{
    int t;
    cin>>t;
    for(int m=0;m<t;m++)
    {

      string s;
      cin>>s;
      for(int i=0;i<s.size();i++)
      {
        int max_index=i;
        int d=s[i]-'0';
        int e=s.size();
        int q=min(e,i+10);
        for(int j=i+1;j<q;j++)
        {
          int x=(s[j]-'0')-(j-i);
           if(x>d)
           {
             max_index=j;
             d=x;
           }
        }

        while(max_index>i)
        {
          swap(s[max_index],s[max_index-1]);
          max_index--;
        }
        s[i]=d+'0';

      }

      cout<<s<<endl;

    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int n = s.size();
        string res = s;

        // cout << n << endl;
        for (int i = 0; i < n; i++)
        {
            int k = -1;
            // cout << res[i] << endl;
            for (int j = i + 1; j < min(i + 10, n); j++)
            {
                // cout << "ta " << res[i] << " " << res[j] << endl;
                if (res[j] + i - j > res[i] and res[j] - j + i >= '0' and res[j] - j + i <= '9')
                {
                    if (k == -1)
                    {
                        k = j;
                        // cout << "Here " << res[k] << endl;
                    }
                    else if (k != -1 and res[j] - j > res[k] - k)
                    {
                        k = j;
                        // cout << "There " << res[k] << endl;
                    }
                }
            }
            // cout << i << " " << k << " ";
            if (k != -1)
            {
                res[k] = res[k] - k + i;
                while (k > i)
                {
                    swap(res[k], res[k - 1]);
                    k--;
                }
            }
            // cout << res << endl;
        }
        cout << res << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int n = s.size();
        string res = s;

        // cout << n << endl;
        for (int i = 0; i < n; i++)
        {
            int k = -1;
            // cout << res[i] << endl;
            for (int j = i + 1; j < n; j++)
            {
                // cout << "ta " << res[i] << " " << res[j] << endl;
                if (res[j] + i - j > res[i] and res[j] - j + i >= '0' and res[j] - j + i <= '9')
                {
                    if (k == -1)
                    {
                        k = j;
                        // cout << "Here " << res[k] << endl;
                    }
                    else if (k != -1 and res[j] - j > res[k] - k)
                    {
                        k = j;
                        // cout << "There " << res[k] << endl;
                    }
                }
            }
            // cout << i << " " << k << " ";
            if (k != -1)
            {
                res[k] = res[k] - k + i;
                while (k > i)
                {
                    swap(res[k], res[k - 1]);
                    k--;
                }
            }
            // cout << res << endl;
        }
        cout << res << endl;
    }
    return 0;
}

Answer: 1


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

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

#include<bits/stdc++.h>
using namespace std;
char judge(string& s,int x)
{
	int ans=0,y;
	if(s.size()-x>10)
	{
		for(int i=x;i<x+10;i++)
		{
			int a=(s[i]-'0')-i+x;
			if(a>ans)
			{
				ans=a;
				y=i;
			}
		}
	}
	else
	{
		for(int i=x,len=s.size();i<len;i++)
		{
			int a=(s[i]-'0')-i+x;
			if(a>ans)
			{
				ans=a;
				y=i;
			}
		}
	}
	if(y==x) return ans+'0';
	else
	{
		for(int i=y;i>=x;i--)
		{
			char c=s[i];
			s[i]=s[i-1];
			s[i-1]=(s[i]-'0'-1)+'0';
		}
		return ans+'0';
	}
}
void sol()
{
	string s;
	string b;
	cin>>s;
	for(int i=0,len=s.size();i<len;i++)
	{
		b+=judge(s,i);
	}
	cout<<b<<endl;
	return;
}
int main()
{
	int t;
	t=0;
	cin>>t;
	while(t--)
	{
		sol();
	}
	return 0;
}

Answer: 1


#include<bits/stdc++.h>

using namespace std;

int main(){

	int t;

	cin>>t;

	while(t--){

	string a;

	cin>>a;

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

		int j=i;

		while(a[j]-1>a[j-1]&&j>0){

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

			a[j-1]--;

			j--;

		}

	}

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

		cout<<a[i];

	}

	cout<<endl;}

	return 0;

}

#include<bits/stdc++.h>

using namespace std;

int main(){

	int t;

	cin>>t;

	while(t--){

	char a[200005];

	cin>>a;

	for(int i=1;i<strlen(a);i++){

		int j=i;

		while(a[j]-1>a[j-1]&&j>0){

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

			a[j-1]--;

			j--;

		}

	}

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

		cout<<a[i];

	}

	cout<<endl;}

	return 0;

}

Answer: 1


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){

    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int n=s.length();
        set<pair<char,int>>st;
        for(int i=0;i<n;i++)st.insert({s[i],n-i});
        while(st.size()){
            auto it=*st.begin();
            char val=it.first;
            int index=n-it.second;
            int i=index;
            st.erase(it);
            if (index==n-1 || s[index+1]=='0')continue;
            int original=(s[index]-'0')*10+s[index+1]-'0';
            int rev=((s[index+1]-'0')-1)*10+(s[index]-'0');
            if (rev>original){
                st.erase({s[index+1],index+1});
                swap(s[index],s[index+1]);
                s[i]=((s[i]-'0')-1)+'0';
                st.insert({s[i],n-i});
                st.insert({s[i+1],n-(i+1)});
            }
        }
        cout<<s<<endl;
    }

  return 0;
}

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){

    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int n=s.length();
        set<pair<char,int>>st;
        for(int i=0;i<n;i++)st.insert({s[i],n-i});
        string org=s;
        while(st.size()){
            auto it=*st.begin();
            char val=it.first;
            int index=n-it.second;
            int i=index;
            st.erase(it);
            if (index==n-1 || s[index+1]=='0')continue;
            int original=(s[index]-'0')*10+s[index+1]-'0';
            int rev=((s[index+1]-'0')-1)*10+(s[index]-'0');
            if (rev>original){
                st.erase({s[index+1],index+1});
                swap(s[index],s[index+1]);
                s[i]=((s[i]-'0')-1)+'0';
                st.insert({s[i],n-i});
                st.insert({s[i+1],n-(i+1)});
            }
            // cout<<s<<endl;
        }
        while(org!=s){
            org=s;
            for(int i=0;i<n;i++)st.insert({s[i],n-i});
            while(st.size()){
                auto it=*st.begin();
                char val=it.first;
                int index=n-it.second;
                int i=index;
                st.erase(it);
                if (index==n-1 || s[index+1]=='0')continue;
                int original=(s[index]-'0')*10+s[index+1]-'0';
                int rev=((s[index+1]-'0')-1)*10+(s[index]-'0');
                if (rev>original){
                    st.erase({s[index+1],index+1});
                    swap(s[index],s[index+1]);
                    s[i]=((s[i]-'0')-1)+'0';
                    st.insert({s[i],n-i});
                    st.insert({s[i+1],n-(i+1)});
                }
                // cout<<s<<endl;
            }
        }
        cout<<s<<endl;
    }

  return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

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() {
    int TESTS = 1; cin >> TESTS;
    while (TESTS --> 0) {
        solve();
        cout << '\n';
    }
    return 0;
}

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

// typedef vector<int> vi;
// typedef vector<vector<int>> vvi;
// typedef vector<long long> vl;
// typedef vector<vector<long long>> vvl;
// typedef vector<bool> vb;
// typedef vector<vector<bool>> vvb;
// typedef map<int, int> mii;
// typedef map<long long, long long> mll;
// typedef pair<int, int> pii;
// typedef pair<long long, long long> pll;
// typedef unsigned long long ull;
// typedef long double ld;
// typedef long long ll;
typedef long long ll;
#define f(i,a,b) for(int i = a; i < b ; i++)
#define geti(n) int n; cin >> n;
#define getll(n) long long int n; cin >> n;
#define gets(s) string s; cin >>s;
#define getv(v,n) vector<int> v(n) ; f(i, 0 ,n) cin >> v[i];
#define srt(v) sort(v.begin(), v.end());
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
    string s;
    cin>>s;
    vector<long long> v(s.size());
    long long n = s.size();
    // for(long long i = 0; i<s.size(); i++){
    //     v[i] = (s[i] - '0');
    // }
    for(long long i = 0; i<n; i++){
        long long max = s[i] - '0';
        long long ind = i;
        for(long long j = i; j<min(n, j+9); j++){
            long long mx = s[i] - '0' - (j - i);
            if(mx > max){
                max = mx;
                ind = j;
            }
        }
        for(long long k = ind; k>i; k--){
            s[k] = s[k-1];
        }
        s[i] = max + '0';
    }
    cout<<s<<endl;
}

int32_t main()
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    for(int i = 0; i < t; i++)
    {
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured:"  << elapsed.count() * 1e-9 << ",seconds.";
    return 0;
}

Answer: 1


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

void solve(){
   string s;
   cin>>s;
   vector<int>digits(s.length());
   //convert string to integer array
   for(int i=0;i<s.length;i++){
    digits[i]=s[i]-'0';
   }
   for(int i=0;i<digits.size();i++){
    int copy_i = i;
    while(copy_i>=0 && digits[copy_i]>0 && (digits[copy_i]>digits[copy_i-1])){
        int temp=digits[copy_i];
        digits[copy_i]=digits[copy_i-1];
        digits[copy_i-1]=temp-1;//***why we use this temp-1 while swqpping instead of using temp */
        if(copy_i>1){
            copy_i--;
        }else{
            break;
        }
    }
   }
   for(int d:digits){
    cout<<digits[d];
   }
}

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

Answer: 1


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

#ifdef LOCAL
#include "/Library/debug/debug.h"
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define MAX                 2e9
#define MIN                 -2e9
#define PI                  acos(-1.0)
#define mid(s, e)           ((s) + ((e) - (s)) / 2)
#define clz(n)              __builtin_clzll(n)
#define nbOfBits(n)         __builtin_popcountll(n)
#define all(x)              (x).begin(), (x).end()
#define endl                '\n'
#define pb                  push_back
#define sz(a)               static_cast<int>((a).size())
#define int                 long long
#define double              long double
#define fi                  first
#define fill(n,arr) for(int i=1;i<=n;i++){ll x;cin>>x;arr.pb(x);}
#define se                  second
#define getunique(v)        {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define getorder(v)         sort(all(v))
#define getlower(s)         transform(s.begin(), s.end(), s.begin(), ::tolower)
#define getupper(s)         transform(s.begin(), s.end(), s.begin(), ::toupper)
#define reverse(s)          reverse(s.begin(), s.end())
#define getmax(ans)         *max_element(all(ans));
#define getmin(ans)         *min_element(all(ans));

using ll = long long;
using vi = vector<int>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vs = vector<string>;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpdd = vector<pdd>;
using si = set<int>;
using ssi = set<si>;
using sb = set<bool>;
using ssb = set<sb>;
using sd = set<double>;
using ssd = set<sd>;
using ss = set<string>;
using sc = set<char>;

// BEFORE coding are you sure you understood the statement correctly?
// PLEASE do not forget to read the sample explanation carefully.
// WATCH out for overflows & RTs in general.
// TEST your idea or code on the corner cases.
// ANALYZE each idea you have thoroughly.

const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1};
const int dy[8]={1, -1, 0, 0, 1, -1, 1, -1};

void solve(){
    string s;cin>>s;
    for(int i=0;i<sz(s);i++){
    for(int i=0;i<sz(s)-1;i++){
    if(s[i] < char(s[i + 1] - 1)){
        s[i + 1]--;
        swap(s[i], s[i + 1]);
    }
    }
}
    cout<<s<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;cin>>t;while(t--)
    solve();
}

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

#ifdef LOCAL
#include "/Library/debug/debug.h"
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define MAX                 2e9
#define MIN                 -2e9
#define PI                  acos(-1.0)
#define mid(s, e)           ((s) + ((e) - (s)) / 2)
#define clz(n)              __builtin_clzll(n)
#define nbOfBits(n)         __builtin_popcountll(n)
#define all(x)              (x).begin(), (x).end()
#define endl                '\n'
#define pb                  push_back
#define sz(a)               static_cast<int>((a).size())
#define int                 long long
#define double              long double
#define fi                  first
#define fill(n,arr) for(int i=1;i<=n;i++){ll x;cin>>x;arr.pb(x);}
#define se                  second
#define getunique(v)        {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define getorder(v)         sort(all(v))
#define getlower(s)         transform(s.begin(), s.end(), s.begin(), ::tolower)
#define getupper(s)         transform(s.begin(), s.end(), s.begin(), ::toupper)
#define reverse(s)          reverse(s.begin(), s.end())
#define getmax(ans)         *max_element(all(ans));
#define getmin(ans)         *min_element(all(ans));

using ll = long long;
using vi = vector<int>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vs = vector<string>;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpdd = vector<pdd>;
using si = set<int>;
using ssi = set<si>;
using sb = set<bool>;
using ssb = set<sb>;
using sd = set<double>;
using ssd = set<sd>;
using ss = set<string>;
using sc = set<char>;

// BEFORE coding are you sure you understood the statement correctly?
// PLEASE do not forget to read the sample explanation carefully.
// WATCH out for overflows & RTs in general.
// TEST your idea or code on the corner cases.
// ANALYZE each idea you have thoroughly.

const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1};
const int dy[8]={1, -1, 0, 0, 1, -1, 1, -1};

void solve(){
    string s;cin>>s;
    for(int i=0;i<sz(s) - 1;i++){
        while(i >= 0 && s[i] < s[i + 1] - 1){
            s[i + 1]--;
                swap(s[i], s[i + 1]);
            i--;
        }
    }
    cout<<s<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;cin>>t;while(t--)
    solve();
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(i+10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

v#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

Answer: 1


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

int32_t main()
{
    cin.tie(0);
    cout.sync_with_stdio(0);
    int test = 1;
    cin >> test;
    while (test--)
    {
        string a;
        cin >> a;
        int n = a.size();
        for (int i = 0; i < n; i++)
        {
            vector<int> p(9 , -1);
            for (int j = i , k = 0; j < min((int) i + 9 , n); j++ , k++)
            {
                p[k] = a[j] - '0' - k;
            }
            int mx = -1 , pos = i;
            for (int m = 0; m < 9; m++)
            {
                if (p[m] > mx)
                {
                    mx = p[m];
                    pos = i + m;
                }
            }
            if (pos != i)
            {
                char ch = a[pos];
                int d = pos - i;
                ch = (ch - '0' - d) + '0';
                a.erase(pos , 1);
                a.insert(i , 1 , ch);
            }
        }
        cout << a << endl;
    }
    return 0;
}

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

int32_t main()
{
    cin.tie(0);
    cout.sync_with_stdio(0);
    int test = 1;
    cin >> test;
    while (test--)
    {
        string a;
        cin >> a;
        int n = a.size();
        for (int i = 0; i < n; i++)
        {
            vector<int> p(8 , -1);
            for (int j = i , k = 0; j < min((int) i + 8 , n); j++ , k++)
            {
                p[k] = a[j] - '0' - k;
            }
            int mx = -1 , pos = i;
            for (int m = 0; m < 8; m++)
            {
                if (p[m] > mx)
                {
                    mx = p[m];
                    pos = i + m;
                }
            }
            if (pos != i)
            {
                char ch = a[pos];
                int d = pos - i;
                ch = (ch - '0' - d) + '0';
                a.erase(pos , 1);
                a.insert(i , 1 , ch);
            }
        }
        cout << a << endl;
    }
    return 0;
}

Answer: 1


t= int(input())
for k in range(t):
    a=input()
    b= len(a)
    for i in range(b):
        if i<(b-8):
            t= i+9
        else:
            t= 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)

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))

Answer: 2


/*************************************************************************************
أَعُوذُ بِاللَّهِ مِنَ الشَّيْطَانِ الرَّجِيمِ
بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ
الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ
الرَّحْمَنِ الرَّحِيمِ
مَالِكِ يَوْمِ الدِّينِ
إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ
اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ
صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ

->  In the name of Allah, the Most Gracious, the Most Merciful.
->  Praise be to Allah, the Lord of all the worlds.
->  The Most Gracious, the Most Merciful.
->  Master of the Day of Judgment.
->  You alone we worship, and You alone we ask for help.
->  Guide us on the Straight Path,
->  the path of those who have received Your grace;
->  not the path of those who have brought down wrath upon themselves,
->  nor of those who have gone astray. (AAMEEN)

**************************************************************************************/
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main (){
    ll t;cin>>t;while(t--){
        string s,s1;cin>>s;
      ll count1=0,count0=0;
      s1.push_back(s[0]);
      for(ll i=1;i<s.size();i++){
        if(s[i]=='1')count1++;
        else if(s[i]=='0')count0++;
        else{
          ll x=(s[i]-'0')-(count1+count0);
          s1=s1+(to_string(x));
        }
      }
      for(ll i=1;i<s.size();i++){
        if(s[i]=='0'||s[i]=='1')s1.push_back(s[i]);
      }

      string s3=s1;
      ll cnt=s1.size();
      for(ll i=1;i<=9;i++){
        for(ll j=0;j<cnt-1;j++){
            if((s1[j]-'0')<((s1[j+1]-'0')-1)){
                ll x=(s1[j+1]-'0')-1;
                 char ch = to_string(x)[0];
                s1[j+1] = ch;
                char c=s1[j];
                 s1[j]=s1[j+1];
                 s1[j+1]=c;
               // swap(s1[j],s1[j+1]);

            }
        }
        if(s3==s1)break;
        s3=s1;
      }
      cout<<s1<<endl;
    }
    return 0;
}

/*************************************************************************************
أَعُوذُ بِاللَّهِ مِنَ الشَّيْطَانِ الرَّجِيمِ
بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ
الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ
الرَّحْمَنِ الرَّحِيمِ
مَالِكِ يَوْمِ الدِّينِ
إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ
اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ
صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ

->  In the name of Allah, the Most Gracious, the Most Merciful.
->  Praise be to Allah, the Lord of all the worlds.
->  The Most Gracious, the Most Merciful.
->  Master of the Day of Judgment.
->  You alone we worship, and You alone we ask for help.
->  Guide us on the Straight Path,
->  the path of those who have received Your grace;
->  not the path of those who have brought down wrath upon themselves,
->  nor of those who have gone astray. (AAMEEN)

**************************************************************************************/
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main (){
    ll t;cin>>t;while(t--){
        string s,s1;cin>>s1;
      // ll count1=0,count0=0;
      // s1.push_back(s[0]);
      // for(ll i=1;i<s.size();i++){
      //   if(s[i]=='1')count1++;
      //   else if(s[i]=='0')count0++;
      //   else{
      //     ll x=(s[i]-'0')-(count1+count0);
      //     s1=s1+(to_string(x));
      //   }
      // }
      // for(ll i=1;i<s.size();i++){
      //   if(s[i]=='0'||s[i]=='1')s1.push_back(s[i]);
      // }

      string s3=s1;
      ll cnt=s1.size();
      for(ll i=1;i<=9;i++){
        for(ll j=0;j<cnt-1;j++){
            if((s1[j]-'0')<((s1[j+1]-'0')-1)){
                ll x=(s1[j+1]-'0')-1;
                 char ch = to_string(x)[0];
                s1[j+1] = ch;
                char c=s1[j];
                 s1[j]=s1[j+1];
                 s1[j+1]=c;
               // swap(s1[j],s1[j+1]);

            }
        }
        if(s3==s1)break;
        s3=s1;
      }
      cout<<s1<<endl;
    }
    return 0;
}

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 ind=i;

            for(int j=i;j<i+9&&j<s.size();j++){

                if(s[ind]<s[j]-(j-i)){

                    ind = j;

                    //cout << s[j] << '\n';

                }

            }

            s.insert(s.begin()+i, (char)(s[ind]-(ind-i)));

            s.erase(s.begin()+ind+1);

                //cout << s << '\n';

        }

        cout << s << '\n';

    }

}

#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 ind = i;

            char k = s[i];

            for(int j=i;j<i+9&&j<s.size();j++){

                if(k<s[j]-(j-i)){

                    ind = j;

                    k = s[j]-(j-i);

                    //cout << s[j] << '\n';

                }

            }

            s.insert(s.begin()+i, (char)(s[ind]-(ind-i)));

            s.erase(s.begin()+ind+1);

            //cout << s << '\n';

        }

        cout << s << '\n';

    }

}

Answer: 2


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

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

    while (true) {
        bool changed = false;

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

        if (!changed) break;
    }

    cout << s << endl;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

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

void solve() {
    string s;
    cin >> s;
    int n = s.length();
    int cnt = 0;

    int prev = INT_MIN;
    int maxi = stoll(s);
    while (prev != maxi) {
        prev = maxi;
        for (int i = 1; i < n - cnt; i++) {
            if ((s[i] - '0') - 1 > (s[i - 1] - '0')) {
                s[i] = (s[i] - '0' - 1) + '0';
                swap(s[i], s[i - 1]);
                maxi = max(maxi, stoll(s));
            }
        }
    }

    cout << maxi << endl;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    if (t) cin >> t; else t = 1;
    while (t--) solve();
    return 0;
}

Answer: 1


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

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

   string s ;
   cin>> s ;
   vector<int>v ;
   for(int i = 0 ; i<s.size() ; i++){
        v.push_back(int(s[i] - '0')) ;
   }
   bool b = false ;
   for(int j = 0 ; j<s.size() ; j++){
   for(int i = 1; i<s.size() ; i++){
        if(v[i]-v[i-1]>=2){
            v[i] -- ;
            swap(v[i] , v[i-1]) ;

        }

   }
   }
   for(int i = 0 ; i<v.size() ; i++){
      cout<<v[i]<<endl ;
   }

   }
}

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

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

   string s ;
   cin>> s ;
   vector<int>v ;
   for(int i = 0 ; i<s.size() ; i++){
      v.push_back(int(s[i] - '0')) ;
   }
   for(int i = 0 ; i<s.size() ; i++){
      int maxi = v[i] ;
      int maxidx = -1 ;

       for (int j = i; j < min(i + 10, (int)v.size()); j++) {
         if(v[j]-j+i> maxi){
            maxi = v[j] -j + i;
            maxidx = j ;
         }
      }
      while(maxidx>i){
         swap(v[maxidx],v[maxidx-1]) ;
         maxidx-- ;
      }
      v[i] = maxi ;

   }
   for(int i = 0 ; i< v.size() ;i++){
      cout<<v[i] ;
   }
   cout<<endl;
}
}

Answer: 2


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

    for i in range(10):
        for j in range(len(s)-1):
            if s[j] + 1 < s[j+1]:
                s[j], s[j+1] = s[j+1] - 1, s[j]

    print("".join(map(str, s)))

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

    for i in range(10):
        for j in range(len(s)-1):
            if s[j] + 1 < s[j+1]:
                s[j], s[j+1] = s[j+1] - 1, s[j]

    print("".join(s))

Answer: 1


// /**
// *    author:  _isomorphism
// *    created: 19.12.2024 16:30:22
// **/
//
// Problem: D. Digital string maximization
// Contest: Codeforces - Codeforces Round 991 (Div. 3)

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)2e18
#define endl '\n'

const int mod = 1e9 + 7;
const int MOD = 998244353;

vector<int> binrep(int n) {
  if (n == 0)
    return {0};
  vector<int> b;
  while (n > 0) {
    b.insert(b.begin(), n % 2);
    n /= 2;
  }
  return b;
}

int binpow(int base, int exp, int m) {
  int result = 1;
  base = base % m;
  while (exp > 0) {
    if (exp % 2 == 1) {
      result = (result * base) % m;
    }
    base = (base * base) % m;
    exp = exp / 2;
  }
  return result;
}

//bool check(int x){
//
//}
//
//int search(int k) {
//  int pos = 0, max = INF;
//  for (int i = max; i >= 1; i /= 2) {
//    while (check(pos + i)) pos += i;
//  }
//  return pos;
//}
//
//int search(int k) {
//    int low = 0, high = INF, ans = -1;
//    while (low <= high) {
//        int mid = low + (high - low) / 2;
//        if (check(mid)) {
//            ans = mid;
//            high = mid - 1;
//        } else {
//            low = mid + 1;
//        }
//    }
//    return ans;
//}

void solve() {
	string s; cin >> s;
	int n = s.length();
	int idx = 0;
	char m = '0', temp;
	string ans = "";
	for(int i = 0; i < n; i++){
		m = '0';
		for(int j = 0; j < 9 && j < n; j++){
			temp = (char)((int)s[j] - j);
			if(m < temp){
				m = temp;
				idx = j;
			}
		}
		ans += m;
		s.erase(s.begin() + idx);
	}
	cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int test = 1;
  cin >> test;

  while (test--) {
    solve();
    cout << '\n';
  }
  return 0;
}

// /**
// *    author:  _isomorphism
// *    created: 19.12.2024 16:30:22
// **/
//
// Problem: D. Digital string maximization
// Contest: Codeforces - Codeforces Round 991 (Div. 3)

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)2e18
#define endl '\n'

const int mod = 1e9 + 7;
const int MOD = 998244353;

vector<int> binrep(int n) {
  if (n == 0)
    return {0};
  vector<int> b;
  while (n > 0) {
    b.insert(b.begin(), n % 2);
    n /= 2;
  }
  return b;
}

int binpow(int base, int exp, int m) {
  int result = 1;
  base = base % m;
  while (exp > 0) {
    if (exp % 2 == 1) {
      result = (result * base) % m;
    }
    base = (base * base) % m;
    exp = exp / 2;
  }
  return result;
}

//bool check(int x){
//
//}
//
//int search(int k) {
//  int pos = 0, max = INF;
//  for (int i = max; i >= 1; i /= 2) {
//    while (check(pos + i)) pos += i;
//  }
//  return pos;
//}
//
//int search(int k) {
//    int low = 0, high = INF, ans = -1;
//    while (low <= high) {
//        int mid = low + (high - low) / 2;
//        if (check(mid)) {
//            ans = mid;
//            high = mid - 1;
//        } else {
//            low = mid + 1;
//        }
//    }
//    return ans;
//}

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;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int test = 1;
  cin >> test;

  while (test--) {
    solve();
    cout << '\n';
  }
  return 0;
}

Answer: 2


#include <iostream>

#include <vector>

#include <iomanip>

#include <cstring>

#include <string>

#include <array>

#include <sstream>

#include <algorithm>

#include <cmath>

#include <climits>

#include<bitset>

#include <tuple>

#include<unordered_set>

#include<map>

#include<set>

#include <stack>

#include <queue>

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

using namespace std;

FILE* stream;

#define ll long long

int d(char c)

{

	return (c - '0');

}

void solve()

{

	string s;

	cin >> s;

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

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

	{

		int best = d(s[i]);

		int pos = i;

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

		{

			if( d(s[j])-(j-i)>best)

			{

				pos = j;

				best = d(s[j])-(j-i);

			}

			while (pos > i)

			{

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

				--pos;

			}

		}

	}

	cout << s << endl;

}

signed main()

{

	FastIO

#ifndef ONLINE_JUDGE

		//For getting input from input.txt file

		freopen_s(&stream, "input.txt", "r", stdin);

	//Prlong longing the Output to output.txt file

	freopen_s(&stream, "output.txt", "w", stdout);

#endif

	ll tc = 1;

	cin >> tc;

	while (tc--) {

		solve();

	}

	return 0;

}

#include <iostream>

#include <vector>

#include <iomanip>

#include <cstring>

#include <string>

#include <array>

#include <sstream>

#include <algorithm>

#include <cmath>

#include <climits>

#include<bitset>

#include <tuple>

#include<unordered_set>

#include<map>

#include<set>

#include <stack>

#include <queue>

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

using namespace std;

FILE* stream;

#define ll long long

int d(char c)

{

	return (c - '0');

}

void solve()

{

	string s;

	cin >> s;

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

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

	{

		int best = d(s[i]);

		int pos = i;

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

		{

			if( d(s[j])-(j-i)>best)

			{

				pos = j;

				best = d(s[j])-(j-i);

			}

		}

		while (pos > i)

		{

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

			--pos;

		}

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

	}

	cout << s << endl;

}

signed main()

{

	FastIO

#ifndef ONLINE_JUDGE

		//For getting input from input.txt file

		freopen_s(&stream, "input.txt", "r", stdin);

	//Prlong longing the Output to output.txt file

	freopen_s(&stream, "output.txt", "w", stdout);

#endif

	ll tc = 1;

	cin >> tc;

	while (tc--) {

		solve();

	}

	return 0;

}

Answer: 2


#include<iostream>
using namespace std;
long long t;
string s;
void zad(){
    cin>>s;
    int right;
    int left;
    for(int i=1; i<s.size(); i++){
        for(int j=i; j>0; j--){
            right = s[j]-'0';
            left = s[j-1]-'0';
            if(right == 0){
                break;
            }
            if(right-1 > left){
                s[j-1] = (right-1)+'0';
                s[j] = left+'0';
            }
        }
    }
    cout<<s<<'\n';
}

int main(){
    cin>>t;
    for(long long i=0; i<t; i++){
        zad();
    }
}

#include<iostream>
using namespace std;
long long t;
string s;
void zad(){
    cin>>s;
    int right;
    int left;
    for(int i=1; i<s.size(); i++){
        for(int j=i; j>0; j--){
            right = s[j]-'0';
            left = s[j-1]-'0';
            if(right-1 > left){
                s[j-1] = (right-1)+'0';
                s[j] = left+'0';
            }else{
                break;
            }
        }
    }
    cout<<s<<'\n';
}

int main(){
    cin>>t;
    for(long long i=0; i<t; i++){
        zad();
    }
}

Answer: 2


#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);

    int t;
    string s;
    cin>>t;

    string ans[t];

    for(int i=0;i<t;i++) {
        cin>>s;

        for(int j=0;j<s.length()-1;j++) {
            int mini = 0;
            int len = s.length();
            int condition = min(9,len-j);

            for(int k=1;k<condition;k++) {
                if(s[j+k] - k > s[j+mini] - mini) {
                    mini = k;
                }
            }

            for(int k=mini;k>0;k--) {
                swap(s[j+k-1],s[j+k]);
                s[j+k-1] -= 1;
            }
        }

        ans[i] = s;
    }

    for(int i=0;i<t;i++) {
        cout<<ans[i]<<endl;
    }

    return 0;
}

#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);

    int t;
    string s;
    cin>>t;

    string ans[t];

    for(int i=0;i<t;i++) {
        cin>>s;
        cout<<"len: "<<s.length()<<endl;

        for(int j=0;j<s.length()-1;j++) {
            int mini = 0;
            int len = s.length();
            int condition = min(9,len-j);

            for(int k=1;k<condition;k++) {
                cout<<"test: "<<s[j+k] - k - '0'<<" ?= "<<s[j+mini] - mini - '0'<<endl;
                if(s[j+k] - k > s[j+mini] - mini) {
                    mini = k;
                }
            }

            for(int k=mini;k>0;k--) {
                swap(s[j+k-1],s[j+k]);
                s[j+k-1] -= 1;
            }
        }

        ans[i] = s;
    }

    for(int i=0;i<t;i++) {
        cout<<ans[i]<<endl;
    }

    return 0;
}

Answer: 1


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

void solve(string &s) {
    int n = s.length();
    deque<int> candidates; // Store indices of potential candidates for swapping

    for (int i = n - 1; i > 0; i--) {
        // Only consider digits that can be decreased (not 0)
        if (s[i] > '0') {
            while (!candidates.empty() && s[candidates.back()] <= s[i]) {
                candidates.pop_back(); // Remove weaker candidates
            }
            candidates.push_back(i); // Add current digit as a candidate
        }

        // Check if there's a valid candidate to swap with the left neighbor
        if (!candidates.empty() && candidates.front() == i) {
            int j = i - 1; // Left neighbor
            if (s[i] - 1 > s[j]) {
                swap(s[i], s[j]);       // Perform the swap
                s[j] = s[j] - 1;       // Decrease the swapped digit
                candidates.pop_front(); // Remove the used candidate
            }
        }
    }
}

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

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

void solve(string &s) {
    bool swapped = true;
    while (swapped) {
        swapped = false;
        for (int i = 1; i < s.length(); i++) {
            if (s[i] > '0' && s[i] - 1 > s[i - 1]) {
                swap(s[i], s[i - 1]);   // Swap the current digit with the left neighbor
                s[i - 1]--;            // Decrease the digit that moved to the left
                swapped = true;        // Mark as swapped to continue processing
            }
        }
    }
}

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

Answer: 2


// B. Just Eat It!

#include<bits/stdc++.h>

#define ll long long

using namespace std;

  double eps=1e-6;

  double sq_rt(double x){

    double lo=1,hi=x;

    double mid;

    while(hi-lo>eps){

        mid=(hi+lo)/2;

        if(mid*mid<x){

            lo=mid;

    }

    else{

        hi=mid;

    }

    }

    cout<<lo<<endl;

  }

int binary(int arr[],int n,int key){

    int s=0;

    int e=n;

    while(s<=e){

        int mid=(e+s)/2;

           if(arr[mid]==key){

            return mid;

           }

           else if(arr[mid]>key){

            e=mid-1;

           }

           else {

            s=mid+1;

           }

    }

    return -1;

}

  int lower_bound(vector<int> &v ,int ele){

    int lo =0, hi=v.size()-1;

    int mid;

    while(hi-lo>1){

        int mid= (hi +lo)/2;

        if(v[mid]<ele){

            lo=mid+1;

        }

        else{

            hi= mid;

        }

    }

    if(v[lo]>=ele){

        return lo;

    }

    if(v[hi]>=ele){

        return hi;

    }

    return -1;

  }

  int upper_bound(vector<int> &v ,int ele){

    int lo =0, hi=v.size()-1;

    int mid;

    while(hi-lo>1){

        int mid= (hi +lo)/2;

        if(v[mid]<=ele){

            lo=mid+1;

        }

        else{

            hi= mid;

        }

    }

    if(v[lo]>ele){

        return lo;

    }

    if(v[hi]>ele){

        return hi;

    }

    return -1;

  }

int gcd(int a, int b)

{

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

}

int lcm(int a,int b){

    int l=max(a,b);

    int s=min(a,b);

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

        if(i%s==0){

            return i;

        }

    }

}

 int prime(int n){

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

        if(n%i==0){

            return 1;

            break;

        }

    }

    return 0;

 }

  void print(map<string,int> & m){

    cout<<m.size()<<endl;

    for(auto & pr : m){

        cout<<pr.first<<" "<<pr.second<<endl;

    }

  }

//------------------------------------Uporer gulo specials function------------------------------------------------------------//

  int main(){

 int te;

 cin>>te;

 while(te--){

     string s;

     cin>>s;

     int si=s.size();

     int arr[si];

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

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

        if(s[i]=='1') arr[i]=1;

        if(s[i]=='2') arr[i]=2;

        if(s[i]=='3') arr[i]=3;

        if(s[i]=='4') arr[i]=4;

        if(s[i]=='5') arr[i]=5;

        if(s[i]=='6') arr[i]=6;

        if(s[i]=='7') arr[i]=7;

        if(s[i]=='8') arr[i]=8;

        if(s[i]=='9') arr[i]=9;

     }

     int r[si];

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

        r[i]=-1;

     }

     int p=0;

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

       int  m=arr[i];

       p=i;

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

            if(j-i<10){

        if((arr[j]+(i-j))>m){

            m=arr[j]+(i-j);

            p=j;

        }

            }

            else break;

        }

        if(p!=i){

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

            arr[k]=arr[k-1];

        }

    }

        r[i]=m;

    r[si-1]=arr[si-1];

 }

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

    cout<<r[i];

} cout<<endl;

  }

}

  //  Balanced Tunnel

//AKASH_CODER//

// B. Just Eat It!

#include<bits/stdc++.h>

#define ll long long

using namespace std;

  double eps=1e-6;

  double sq_rt(double x){

    double lo=1,hi=x;

    double mid;

    while(hi-lo>eps){

        mid=(hi+lo)/2;

        if(mid*mid<x){

            lo=mid;

    }

    else{

        hi=mid;

    }

    }

    cout<<lo<<endl;

  }

int binary(int arr[],int n,int key){

    int s=0;

    int e=n;

    while(s<=e){

        int mid=(e+s)/2;

           if(arr[mid]==key){

            return mid;

           }

           else if(arr[mid]>key){

            e=mid-1;

           }

           else {

            s=mid+1;

           }

    }

    return -1;

}

  int lower_bound(vector<int> &v ,int ele){

    int lo =0, hi=v.size()-1;

    int mid;

    while(hi-lo>1){

        int mid= (hi +lo)/2;

        if(v[mid]<ele){

            lo=mid+1;

        }

        else{

            hi= mid;

        }

    }

    if(v[lo]>=ele){

        return lo;

    }

    if(v[hi]>=ele){

        return hi;

    }

    return -1;

  }

  int upper_bound(vector<int> &v ,int ele){

    int lo =0, hi=v.size()-1;

    int mid;

    while(hi-lo>1){

        int mid= (hi +lo)/2;

        if(v[mid]<=ele){

            lo=mid+1;

        }

        else{

            hi= mid;

        }

    }

    if(v[lo]>ele){

        return lo;

    }

    if(v[hi]>ele){

        return hi;

    }

    return -1;

  }

int gcd(int a, int b)

{

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

}

int lcm(int a,int b){

    int l=max(a,b);

    int s=min(a,b);

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

        if(i%s==0){

            return i;

        }

    }

}

 int prime(int n){

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

        if(n%i==0){

            return 1;

            break;

        }

    }

    return 0;

 }

  void print(map<string,int> & m){

    cout<<m.size()<<endl;

    for(auto & pr : m){

        cout<<pr.first<<" "<<pr.second<<endl;

    }

  }

//------------------------------------Uporer gulo specials function------------------------------------------------------------//

  int main(){

 int te;

 cin>>te;

 while(te--){

     string s;

     cin>>s;

     int si=s.size();

     int arr[si];

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

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

        if(s[i]=='1') arr[i]=1;

        if(s[i]=='2') arr[i]=2;

        if(s[i]=='3') arr[i]=3;

        if(s[i]=='4') arr[i]=4;

        if(s[i]=='5') arr[i]=5;

        if(s[i]=='6') arr[i]=6;

        if(s[i]=='7') arr[i]=7;

        if(s[i]=='8') arr[i]=8;

        if(s[i]=='9') arr[i]=9;

     }

     int r[si];

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

        r[i]=-1;

     }

     int p=0;

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

       int  m=arr[i];

       p=i;

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

            if(j-i<10){

        if((arr[j]+(i-j))>m){

            m=arr[j]+(i-j);

            p=j;

        }

            }

        }

        if(p!=i){

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

            arr[k]=arr[k-1];

        }

    }

        r[i]=m;

    r[si-1]=arr[si-1];

 }

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

    cout<<r[i];

} cout<<endl;

  }

}

  //  Balanced Tunnel

//AKASH_CODER//

Answer: 1


#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<unordered_map>
using namespace std;

int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
       string s;
       cin>>s;
       vector<char> vec;

       for(int j=0;j<s.length();j++){
              for(int k=j;k>max(0,k-10);k--){
                     if(s[k]-s[k-1]>1){
                        int temp=s[k]-1;
                        s[k]=s[k-1];
                        s[k-1]=temp;
                     }
              }
       }
           cout<<s<<endl;
    }
    return 0;
}

#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<unordered_map>
using namespace std;

int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
       string s;
       cin>>s;
       vector<char> vec;

       for(int j=1;j<s.length();j++){
        // int h=s[j-1]-48;

              for(int k=j;k>0;k--){
                //    cout<<"k";
                     if(s[k]-s[k-1]>1){
                        int temp=s[k]-1;
                        s[k]=s[k-1];
                        s[k-1]=temp;
                     }
                     else{
                        break;
                     }
              }
            //   cout<<" ";
            //    cout<<s<<endl;
       }
           cout<<s<<endl;
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

void sol(){

     string s;

     cin>>s;

     int n = s.size();

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

        int count=0;

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

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

         if(int(s[i]-'0')-int(s[i-1]-'0')>=2){

            count++;

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

            s[i-1]= char(int(s[i-1])-1);

         }

     }

     if(count==0)break;

    }

    cout<<s<<endl;

}

int main() {

  int t;

  cin>>t;

  while(t--){

    sol();

  }

  return 0;

}

#include <bits/stdc++.h>

using namespace std;

void sol(){

     string s;

     cin>>s;

     int n = s.size();

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

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

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

         if(int(s[i]-'0')-int(s[i-1]-'0')>=2){

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

            s[i-1]= char(int(s[i-1])-1);

         }

     }

    }

    cout<<s<<endl;

}

int main() {

  int t;

  cin>>t;

  while(t--){

    sol();

  }

  return 0;

}

Answer: 1


#include<iostream>
using namespace std;

int main()
{
    string s;
    int num;
    cin>>num;
    while(num--)
    {
        cin>>s;
        int swapped;
        do {
            swapped = 0;
            for (int i = 1; i < s.size(); i++) {
                if (s[i] > s[i - 1] + 1) {

                    char temp = s[i - 1];
                    s[i - 1] = s[i] - 1;
                    s[i] = temp;
                    swapped = 1;
                }
            }
        } while (swapped);
        cout<<s<<endl;
    }
    return 0;
}

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

string max_str(string &s)
{
    string maxstr=s;
    bool update;
    int i=1;
    while(true)
    {
        if(i==s.size())
        {
            i=1;
            if(!update)
            {
                return maxstr;
            }
        }
        update=false;
        if(s[i]!='0')
        {
            char temp=s[i-1];
            int num=s[i]-'0';
            num=num-1;
            s[i-1]=num+'0';
            s[i]=temp;
        }
        if(maxstr<s)
        {
            maxstr=s;
            update=true;
        }
        else
        {
            s=maxstr;
        }
        i++;
    }
}
int main()
{
    string st;
    int num;
    vector<string> myvec;
    cin>>num;
    while(num>0)
    {
        cin>>st;
        myvec.push_back(max_str(st));
        num--;
    }
    for(auto str:myvec)
    {
        cout<<str<<endl;
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

void solve() {

string s;

cin >> s;

const int n = s.length();

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

for (int j{i}; j && s[j] > s[j - 1] + 1; --j) {

s[j] -= 1;

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

}

}

cout << s << '\n';

}

int main() {

cin.tie(0)->sync_with_stdio(0);

int t; cin >> t; while (t--)

solve();

}

#include <bits/stdc++.h>

using namespace std;

void solve() {

string s;

cin >> s;

const int n = s.length();

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

for (int j{i}; j; --j) {

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

s[j] -= 1;

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

}

}

}

cout << s << '\n';

}

int main() {

cin.tie(0)->sync_with_stdio(0);

int t; cin >> t; while (t--)

solve();

}

Answer: 1


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// Utilities
#define endl '\n'
#define int long long
#define no cout << "NO"
#define ye cout << "YES"
#define print(...) cout << (__VA_ARGS__)
#define printsp(...) cout << (__VA_ARGS__) << ' '
#define println(...) cout << (__VA_ARGS__) << '\n'

// Looping Mechanism
#define forEach(x, ds) for (auto &x : ds)
#define forr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rforr(i, a, b) for (int i = (a); i >= (b); --i)

// Shortcut Functions
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define getMx(x) *max_element(all(x))
#define getMn(x) *min_element(all(x))
#define rall(x) (x).rbegin(), (x).rend()
#define getSum(x) accumulate(all(x), 0ll)
#define cntBit(x) __builtin_popcountll(x)
#define dsFind(x, ds) (ds.find(x) != ds.end())
#define lb(x, ds) lower_bound(ds.begin(), ds.end(), x)
#define ub(x, ds) upper_bound(ds.begin(), ds.end(), x)

// Data Types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

// Constant Varialbes
const int inf = 1e9;
const int llinf = 1e18;
const int mod = 1e9 + 7;

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

    auto getNum = [&](char ch)
    {
        return ch - '0';
    };

    int n = sz(s);
    forr(i, 0, n - 1)
    {
        int best = getNum(s[i]), pickIdx = i,j = i;
        while (j <= 9 && j < n)
        {
            int curr = getNum(s[j]) - (j - i);
            if (curr > best)
                best = curr, pickIdx = j;
            j++;
        }
        while (pickIdx > i)
            s[pickIdx] = s[pickIdx - 1], pickIdx--;
        print(best);
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--)
    {
        solve();
        cout << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// Utilities
#define endl '\n'
#define int long long
#define no cout << "NO"
#define ye cout << "YES"
#define print(...) cout << (__VA_ARGS__)
#define printsp(...) cout << (__VA_ARGS__) << ' '
#define println(...) cout << (__VA_ARGS__) << '\n'

// Looping Mechanism
#define forEach(x, ds) for (auto &x : ds)
#define forr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rforr(i, a, b) for (int i = (a); i >= (b); --i)

// Shortcut Functions
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define getMx(x) *max_element(all(x))
#define getMn(x) *min_element(all(x))
#define rall(x) (x).rbegin(), (x).rend()
#define getSum(x) accumulate(all(x), 0ll)
#define cntBit(x) __builtin_popcountll(x)
#define dsFind(x, ds) (ds.find(x) != ds.end())
#define lb(x, ds) lower_bound(ds.begin(), ds.end(), x)
#define ub(x, ds) upper_bound(ds.begin(), ds.end(), x)

// Data Types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

// Constant Varialbes
const int inf = 1e9;
const int llinf = 1e18;
const int mod = 1e9 + 7;

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

    auto getNum = [&](char ch)
    {
        return ch - '0';
    };

    int n = sz(s);
    forr(i, 0, n - 1)
    {
        int best = getNum(s[i]), pickIdx = i,j = i;
        while (j-i <= 9 && j < n)
        {
            int curr = getNum(s[j]) - (j - i);
            if (curr > best)
                best = curr, pickIdx = j;
            j++;
        }
        while (pickIdx > i)
            s[pickIdx] = s[pickIdx - 1], pickIdx--;
        print(best);
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--)
    {
        solve();
        cout << endl;
    }

    return 0;
}

Answer: 2