ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: c++17 

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

typedef long long ll;

void solve() {

    string s ; cin>>s;
    ll cnt2 = 0 , cnt3 = 0 , sum = 0;
    for (int i = 0 ; i < s.size() ; ++i) {
        if (s[i] == '3') ++cnt3;
        else if (s[i] == '2') ++cnt2;
        sum += s[i]-48;
    }
       if (cnt2 >= 5) {
        cnt2 -= 2 ;
        cnt3 += (cnt2*2)/6;
        cnt2 -= (cnt2*2)/6*3;
        cnt2 += 2;
       }
          while (sum % 9 != 0 && cnt3 > 0) {
                 if ((sum + 2) % 9 == 0 && cnt2 > 0) {
                 sum += 2;
                 --cnt2;
                 break;
              }
              else if ((sum + 4) % 9 == 0 && cnt2 > 1) {
                sum += 4;
                cnt2 -= 2;
                break;
              }
              else if ((sum + 8) % 9 == 0 && cnt2 > 3) {
                sum += 8;
                cnt2 -= 4;
              }
              else {
              cnt3--;
              sum += 6;
              }
          }
        if (sum % 9 == 0 || ((sum + 2) % 9 == 0 && cnt2 > 0) || ((sum + 4) % 9 == 0 && cnt2 > 1) || ((sum + 6) % 9 == 0 && cnt2 > 2) || ((sum + 8) % 9 == 0 && cnt2 > 3)) cout<<"YES";
        else cout<<"NO";
        cout<<endl;
    }
int main()
{
    int t ; cin>>t;
    while (t--) {
    solve();
    }
    return 0;
}


Solution 2 (runtime: 61 ms):
// Source: https://usaco.guide/general/io





/*

وَمَا بِكُم مِّن نِّعْمَةٍ فَمِنَ اللَّهِ ۖ ثُمَّ إِذَا مَسَّكُمُ الضُّرُّ فَإِلَيْهِ تَجْأَرُونَ

[ النحل: 53]

*/

#include <bits/stdc++.h>



#define ll long long

#define pii pair<int,int>

#define pll pair<ll,ll>

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

#define rall(v) v.rbegin(),v.rend()

#define Bahy ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

using namespace std;



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

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



const long double EPS = 1e-9;

const ll OO = 0X3F3F3F3F3F3F3F3F, MOD = 998244353;

const int N = 1e6 + 10, M = 100 + 5, INF = 1e9 + 10;



void solve() {

    string s;cin>>s;

    ll sum = 0;

    int cnt1 = 0, cnt2 = 0;

    for(auto c:s){

        sum+=(c-'0');

        if(c == '2')cnt1++;

        else if(c == '3')cnt2++;

    }

    bool ok[10] = {0};

    int v1[9] = {0, 2, 4, 6, 8, 1, 3, 5, 7};

    int v2[3] = {0, 3, 6};

    for(int i=0;i<=min(cnt2, 2);i++){

        int st = v2[i];

        for(int j = st; j <= st+min(cnt1, 8); j++){

            ok[v1[j%9]] = 1;

        }

    }



    int rem = (9 - sum%9)%9;

    if(ok[rem])cout<<"YES\n";

    else cout<<"NO\n";

}



int main() {

    Bahy

    int t = 1;

    cin >> t;

    while (t--)

        solve();

}
Problem: 2049B - pspspsps
Language: c++17 

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long, long>;
using pii = pair<int, int>;

string s;
int n;

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

	if (s[0] == 's') s[0] = '.';
	if (s[n-1] == 'p') s[n-1] = '.';

	bool pp = false;
	bool ss = false;
	for (char& c : s) {
		if (c == 'p') { pp = true;}
		if (c == 's') { ss = true;}
	}
	if (pp && ss) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
}

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


Solution 2 (runtime: 61 ms):
#include<bits/stdc++.h>

#define pii pair<int,int>

#define fi first

#define se second

#define ll long long

#define int long long

#define endl '\n'

#define ull unsigned long long

using namespace std;

const int N=505;

char s[N];int n;

void work(){

    cin>>n;

    cin>>(s+1);

    int fl=0;int cs=0,cp=0;

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

        if(s[i]=='p'){if(fl==0);cp++;}

        if(s[i]=='s'){if(fl==1)fl++;cs++;}

    }

    if(fl==2){

        cout<<"NO"<<endl;

        return;

    }

    if(cs==0||cp==0||(cp==1&&s[n]=='p')||(cs==1&&s[1]=='s'))

        cout<<"YES"<<endl;

    else cout<<"NO"<<endl;

    return;

}

signed main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    int T=1;

    cin>>T;

    while(T--)work();

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <iostream>
#include <vector>
#include <string>
#include<bits/stdc++.h>


using namespace std;
using ll = long long;

template<class T>
void cl(vector<T> &vec,ll a,ll b){
    ll min_ = min(vec[a],vec[b]);
    vec[a]-=min_;
    vec[b]-=min_;
}
template<class T>
void ret(vector<T> &vec1){
    // checker for sorted

    for(ll j = 0; j < vec1.size()-1; ++j){

        if(vec1[j]<=vec1[j + 1]){
            cl(vec1,j,j+1);
        }
    }

    cout << (is_sorted(vec1.begin(), vec1.end()) ? "YES" : "NO") << '\n';
}




int main(){
    cin.tie(nullptr) -> sync_with_stdio(false);
    ll n;
    cin >> n;
    
    for(ll s = 0; s < n; s++){
        ll sn;
        cin >> sn;
        vector<ll> vec(sn);
        for (ll &t : vec){
            cin >> t;
        }
        ret(vec);

        
    }
    return 0;


}

Solution 2 (runtime: 343 ms):
#include<bits/stdc++.h>

using namespace std;



void solve(){

int n;

cin >> n;

vector<int>a(n+1);

for(int i = 1;i<=n;i++)cin >> a[i];

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

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

int t = min(a[i],a[i+1]);

a[i] -= t;

a[i+1] -= t;

}else{

cout << "NO" << endl;

return ;

}

}

cout << "YES" << endl;

}



int main(){

int t;

cin >> t;

while (t--){

solve();

}

}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <iostream>

using namespace std;

typedef long long ll;
const int N = 1010;

int T, n;
ll a[N];

ll getSum(int num) {
    ll sum = 0;
    for (int i = 1; i <= num; i++)
        sum += a[i];
    return abs(sum);
}

int main() {
    ios::sync_with_stdio(false);
    cin >> T;
    while(T--) {
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        if (n == 1) {
            cout << a[1] << endl;
            continue;
        }
        ll ans = -1e18, sum = 0;
        for (int i = 1; i <= n; i++)
            sum += a[i];
        ans = max(ans, sum);
        for (int i = 1; i <= n; i++) {
            int num = n - i;
            for (int j = 1; j <= num; j++)
                a[j] -= a[j + 1];
            ans = max(ans, getSum(num));
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<ll> a(n);  
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        
        if (n == 1) {
            cout << a[0] << endl; 
            continue;
        }

        ll ans = 0;  // Store max encountered value
        for (int i = 0; i < n; i++) {
            ans += a[i];  // Initialize with sum of elements
        }

        while (a.size() > 1) {
            ans = max(ans, abs(a.back() - a[0]));  // Correct indexing
            
            vector<ll> b;
            for (size_t i = 0; i < a.size() - 1; i++) {
                b.push_back(a[i + 1] - a[i]);
            }
            a = b;  
        }
        
        cout << max(ans, abs(a[0])) << endl;
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <iostream>
#include <string>
using namespace std;

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

Solution 2 (runtime: 108 ms):
#include <iostream>

using namespace std;



bool check(string a)

{

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

	{

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

		{

			continue;

		}

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

		{

			return true;

		}

	}

	return false;

}



int main()

{

	int t;

	cin >> t;

	while (t--)

	{

		string s;

		cin >> s;

		while (check(s) == true)

		{

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

			{

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

				{

					continue;

				}

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

				{

					s[i + 1] -= 1;

					char temp = s[i];

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

					s[i + 1] = temp;

				}

			}

		}

		cout << s << endl;

	}

	

	return 0;

}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\n'

const int N = 3e5 + 10;

int n , m;
int k;

void solve()
{
    int l, r;
    cin >> l >> r;
    k = 0;
    int n = r;
    while (n) {
        k++;
        n >>= 1;
    }
    // cout << k << endl;
    int ans = 0, ans1 = 0;
    bool c = 0;
    for (int i = k - 1 ;i >= 0; i -- ) {
        if(c){
            ans ^= 1 << i;
        }
        if(!c){
            if(r & 1 << i && l & 1 << i){
                ans ^=  1 << i;
                ans1 ^= 1 << i;
            }
            else if((r & 1 << i )== 0 && (l & 1 << i )== 0)
                continue;
            else {
                ans1 ^= 1 << i;
                c = 1;
            }
        }
      //  cout << ans << ' ' << ans1 << endl;
    }
    if(ans1 == l || ans == l)
        cout << ans << ' ' << ans1 << ' ' << r << endl;
    else
        cout << ans << ' ' << ans1 << ' ' << l << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
}

Solution 2 (runtime: 109 ms):
for _ in range(int(input())):
    l,r=map(int,input().split())
    low=1
    pow=0
    while low<=l:
        low*=2
        pow+=1
    l2=pow-1
    while r>=low:
        low*=2
        pow+=1
    r2=pow-1
    if l2==-1:
        print(0,1<<r2,(1<<r2)-1)
    elif r2==l2:
        x=set()
        k=r-1<<r2
        t=l
        i=0
        while k>0:
            if k&1==0:
                x.add(i)
            if t&1:
                x.add(i)
            i+=1
            k>>=1
            t>>=1
        num=0
        for k in x:
            num+=1<<k
        print(l,r,num+(1<<r2))
    elif r2-l2==1:
        a=1<<r2
        b=a-1
        if a-l>1:
            print(a-2,a-1,a)
        else:
            print(b,a,a+1)
    else:
        a=1<<r2
        b=(1<<r2) -1
        c=l
        print(c,b,a)
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 77 ms):
#define mod 1000000007 
#define loop(i, n) for(int i = 0; i < n; i++)
#define rloop(i, n) for(int i = n-1; i >= 0; i--)
#define range(i, a, b) for(int i = a; i <= b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define full(x) (x).begin(), (x).end()
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define sqr(x) ((x) * (x))
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define ll long long
#define vfill(v, n) fill(all(v), n)
#define floor_div(a, b) std::floor(static_cast<double>(a) / (b))
#define ceil_div(a, b) std::ceil(static_cast<double>(a) / (b))

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

void solve(){
    int n,k;
    cin >> n >> k;

    vi a(n);
    loop(i,n){
        cin>>a[i];
    }
    sort(a.begin(), a.end());
    vector<int> v = {1};
    range(i,1,n-1) {
        if (a[i] == a[i - 1])   v.back()++;
        else    v.emplace_back(1);
    }

    sort(v.begin(),v.end());
    int m=v.size();

    loop(i,m){
        if(v[i]<=k) k-=v[i];
        else{
            cout << m-i << endl;
            return;
        }
    }
    cout << 1 << endl;
}
int main(){

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

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

    return 0;
}

Solution 2 (runtime: 249 ms):
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long t;
    cin>>t;
    while(t--)
    {
        long long n,k;cin>>n>>k;
        map<long long ,long long>m;
        for(long long i=0;i<n;i++)
        {
            long long temp;cin>>temp;
            m[temp]++;
        }
        vector<long long>v;
        for(auto x:m)
        {
            v.push_back(x.second);
        }
        sort(v.begin(),v.end());
        long long i=0;
        while(k>0 && i<v.size()-1)
        {
            if(v[i]<=k)
            {
                k-=v[i];
                v[i]=-1;
            }
            i++;
        }
        long long ans=0;
        for(long long i=0;i<v.size();i++)
        {
            if(v[i]!=-1)
            {
                ans++;
            }
        }
        cout<<ans<<endl;
    }

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 359 ms):
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int a[N];
bool solve()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<n;i++)
    {
        a[i]-=a[i-1];
        if(a[i]>a[i+1]) return false;
    }
    return true;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        if(solve()) cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}

Solution 2 (runtime: 93 ms):
/*

 * Problem: D. Subtract Min Sort

 * URL: https://codeforces.com/problemset/problem/2060/D

 * Time Limit: 2.0 seconds

 * Memory Limit: 256 MB

 */



#include <bits/stdc++.h>

using namespace std;

#define int long long



void solve() {

    // Your solution here

    int n;cin>>n;

    int arr[n];

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

		cin>>arr[i];

	}bool flag=1;

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

		if(arr[i]>arr[i+1]){flag=0;break;}

		else if(arr[i]<arr[i+1]){arr[i+1]-=arr[i];arr[i]=0;}

		else{arr[i+1]=0;arr[i]=0;}

	}

	if(flag)cout<<"YES\n";

	else cout<<"NO\n";

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    

    int t = 1;

    // Uncomment for multiple test cases

    cin >> t;

    

    while (t--) {

        solve();

    }

    

    return 0;

}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 202 ms):
#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;  // number of testcases

    cin >> t;

    for (int i = 0; i < t; i++) {
        int n, k, A, changes = 0;

        cin >> n >> k;

        vector<int> a, unique, occurence;

        for (int j = 0; j < n; j++) {   // input array
            cin >> A;
            a.push_back(A);
        }

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

        unique.push_back(a[0]);
        occurence.push_back(1);

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

            if (unique.back() != a[j]) {
                unique.push_back(a[j]);
                occurence.push_back(1);
            } else {
                occurence.back() += 1;
            }
        }

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

        for (int j = 0; j < occurence.size() - 1; j++) {
            if (k - occurence[j] >= 0) {
                changes++;
                k -= occurence[j];
            } else {
                break;
            }
        }

        cout << occurence.size() - changes << endl;
    }
}

Solution 2 (runtime: 78 ms):
#include <bits/stdc++.h>
#include <cassert>

#define endl "\n"
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double lld;

using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<vector<int>>;

#define endl "\n"
#define all(x) (x).begin(),  (x).end()
#define inf 1e9


 
const int mod = (int)1e9 + 7;

void solve(){
	int n, k;
	cin >> n >> k;

	vector<int> arr(n);


	for(int i = 0; i < n; i++){
		cin >> arr[i];
	}

	sort(arr.begin(), arr.end());
	vi arr1;
	int cnt = 1;

	for(int i = 1; i < n; i++){
		if(arr[i] == arr[i-1]){
			cnt++;
		}
		else{
			arr1.emplace_back(cnt);
			cnt = 1;
		}
	}
	arr1.emplace_back(cnt);

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

	int m = arr1.size();

	for(int i = 0; i < m-1; i++){
		if(arr1[i] > k){
			cout << m - i << endl;
			return;
		}
		k -= arr1[i];
	}
	cout << 1 << endl;

}

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

	int t = 1;
	cin >> t;

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

	return 0;	
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 108 ms):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll mod = 1e9 + 7;

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

    int t;
    cin >> t;
    while (t--) {
        ll l, r;
        cin >> l >> r;

        ll ans1 = l;
        ll ans2 = 0;
        ll ans3 = r;
        ll msb = -1;

        
        for (ll i = 0; i < 60; i++) {
            if ((r & (1LL << i)) != 0) {
                msb = i;
            }
        }

        
        for (ll i = msb; i >= 0; i--) {
            if ((r & (1LL << i)) != 0 && (l & (1LL << i)) == 0) {
                ll temp = ans2;
                temp = ans2 | (1LL << i); 
                if (r == temp) {
                    ans2 = l;
                    for (int j = i - 1; j >= 0; j--) {
                        if ((l & (1LL << j)) == 0) {
                            ans2 = ans2 | (1LL << j);
                        }
                    }
                } else {
                    ans2 = ans2 | (1LL << i);
                    for(int j=i-1;j>=0;j--)
                    {
                      if ((r & (1LL << j)) == 0 && (l & (1LL << j)) == 0)
                      {
                        ans1=ans1|(1LL<<j);
                      }
                    }
                }
                break;
            } else {
                if ((r & (1LL << i)) != 0) {
                    ans2 = (ans2 | (1LL << i));
                }
            }
        }

        cout << ans1 << " " << ans2 << " " << ans3 << endl;
    }

    return 0;
}


Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;
void solve()
{
long long l,r;
cin>>l>>r;
long long ans=0;
long long a,b,c;
for(int i=30;i>=0;i--){
  long long b1=0,b2=0;
  if(l&(1LL<<i)) b1 =1 ;
  if(r&(1LL<<i)) b2 =1 ;  
  if(b1==b2){
    ans += b1*(1LL<<i);
  }else {
    a = ans + (1LL<<i);
    b = a-1;
    break;
  }
}
for(long long i = r ;i>=l;i--){
  if(i!=a && i!=b) {
    c= i;
    break;
    }

}
cout<<a<<" "<<b<<" "<<c<<"\n";


}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    int t = 1;
    cin >> t;
    // cout<<t<<endl;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long 
int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t; cin>>t;
    while(t--){
        string s; cin>>s;
        int n=s.size();
        for(int i=1;i<n;i++){
            int j=i-1,cur=i;
            while(j>=0 && s[cur]>'0' && s[cur]>s[j]+1){
                s[cur]--;
                swap(s[cur],s[j]);
                cur--;
                j=cur-1;
            }
        }
        cout<<s<<endl;
    }
}

Solution 2 (runtime: 233 ms):
#pragma region usual_defs

#include <iostream>

#include <string.h>

#include <algorithm>

#include <cmath>



// this is the only reason I like C++ (sorry, java, try to be more fun next time):

#define meow cin

#define murr cout

#define lol long long

#define lmao unsigned long long

#define el '\n'

#define none \

    {        \

    }

#define kaput return

#define kill break

#define nah continue

using namespace std;

#pragma endregion usual_defs

// #define SYNC

#define i_like_cats

// WHY AM I SO DUMBBBBB

int s[200000];

char ch;

int n, j, t;

void cat()

{

    n = 0;

    while (true)

    {

        meow.get(ch);

        if (ch == '\n')

            kill;

        s[n++] = ch - '0';

    }

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

    {

        j = i;

        while (j > 0 && s[j - 1] < s[j] - 1)

        {

            t = s[j - 1];

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

            s[j--] = t;

        }

    }

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

        murr << s[i];

    murr << el;

}



#pragma region main

#ifdef i_like_cats

int INPUT_SETS_COUNT;

#endif

int main()

{

    ios_base::sync_with_stdio(false);

#ifndef SYNC

    meow.tie(NULL);

#endif

#ifdef i_like_cats

    meow >> INPUT_SETS_COUNT;

    meow.ignore();

    while (INPUT_SETS_COUNT--)

        cat();

#endif

#ifndef i_like_cats

    cat();

#endif

}

#pragma endregion main
Problem: 2050D - Digital string maximization
Language: c++17 

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


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

//void solve(){
//    string s;
//    cin >> s;
//    int n = s.size();
//    vimo<int> backMax(n), backMaxIdx(n);
//    backMax[n-1] = s[n-1]-'0';
//    backMaxIdx[n-1] = n-1;
//    for(int i = n; i >= 0; i--){
//        if(backMax[i+1]-1 > s[i]-'0'){
//            backMax[i] = backMax[i+1]-1;
//            backMaxIdx[i] = backMaxIdx[i+1];
//        }else{
//            backMax[i] = s[i]-'0';
//            backMaxIdx[i] = i;
//        }
//    }
//
//
//
//    return;
//}

void solve(){
    string s;
    cin >> s;
    int len = s.size();
    while(true){
        bool didOne = false;
        for(int i = 1; i < len; i++){
            int dig = s[i] - '0';
            int prevDig = s[i-1] - '0';
            if(dig-1 > prevDig){
                s[i-1] = dig-1 + '0';
                s[i] = prevDig + '0';
                didOne = true;
            }
        }
        if(!didOne){ break; }
    }
    cout << s << '\n';
    return;
}

int main(){
//    ios_base::sync_with_stdio(false);
//    cin.tie(nullptr);
//    cout.tie(nullptr);
//solve();

    int T;
    cin >> T;
    for(int t = 1; t <= T; t++){
        solve();
    }
    return 0;
}