ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 687 ms):
#include <stdio.h>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=2e5+200;
set<int>e[N];
vector<pair<int,int>>v;
int a[N];
bool cmp(pair<int,int>x,pair<int,int>y){
	return x.second>y.second;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		ll n;
		cin>>n;
		v.clear();
		for(int i=1;i<=n;++i){
			a[i]=0;
			e[i].clear();
		}
		int mx=0;
		for(int i=1;i<n;++i){
			int x,y;
			cin>>x>>y;
			e[x].insert(y);
			e[y].insert(x);
			a[x]++;
			a[y]++;
		}
		for(int i=1;i<=n;++i) v.push_back({i,a[i]});
		sort(v.begin(),v.end(),cmp);
		int ans=1;
		int cnt=0;
		while(cnt<n-1&&v[cnt].second==v[cnt+1].second) cnt++;
		if(cnt==0){
			ans+=v[0].second-1;
			ans+=v[1].second-1;
			int f=1;
			cnt=1;
			while(cnt<n-1&&v[cnt].second==v[cnt+1].second) cnt++;
			for(int i=1;i<=cnt;++i){
				if(e[v[i].first].find(v[0].first)==e[v[i].first].end()){
					f=0;
					break;
				}
			}
			if(f) ans--;
		}else if(cnt==1){
			ans+=v[0].second-1;
			ans+=v[1].second-1;
			if(e[v[0].first].find(v[1].first)!=e[v[0].first].end())ans--;
		}else{
			ans+=v[0].second-1;
			ans+=v[1].second-1;
		}
		cout<<max(ans,0)<<'\n';
	} 
	return 0;
}

Solution 2 (runtime: 1577 ms):

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define nl "\n"
#define ull unsigned long long
#define rv return void
#define str string
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define vec vector
#define fixed(n) fixed << setprecision(n)
#define Moageza ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
using namespace std;
const ll mod = 1e9+7;
//////////////////////////////////////////////////////
int a;
vec<vec<int>>v;
void solve(){
  cin>>a;
  v.assign(a+5,vec<int>());
  map<int,int>m;
  for(int i=0;i<a-1;i++){
    int x,y;cin>>x>>y;
    v[x].push_back(y);
    v[y].push_back(x);
    m[x]++;m[y]++;
  }
  set<pair<int,int>>p;
  for(int i=1;i<=a;i++)p.insert({m[i],i});
  int c=0;
    for(int i=1;i<=a;i++){
        p.erase({m[i],i});
        for(auto it:v[i]){
            c=max(c,m[i]+m[it]-2);
            p.erase({m[it],it});
        }
        if(p.size())c=max(c,m[i]+p.rbegin()->first-1);
        p.insert({m[i],i});
        for(auto it:v[i]) p.insert({m[it],it});
    }
    cout<<c;
}
int main()
{
   Moageza
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
     cin >> t;
    while (t--) {
        solve();
        cout << nl;
    }
    return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;
int t,l,r,ans;
int main(){
	cin>>t;
	while(t--){
		scanf("%d%d",&l,&r);
		for(int i=29;i>=0;i--){
			if((l&(1<<i))^(r&(1<<i))){
				ans=r&((1<<30)-(1<<i));
				break;
			}
		}
		printf("%d %d %d\n",ans,ans-1,ans==r?l:r);
	}
	return 0;
}

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

#define endl '\n'
#define ll long long
#define lld long double
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define tc    \
    ll t;     \
    cin >> t; \
    while (t--)
#define IOS ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

void solve()
{
    ll l, r;
    cin >> l >> r;
    ll bits = 0;
    vector<int> v1, v2;
    for (int i = 31; i >= 0; i--)
    {
        v1.push_back((l >> i) & 1);
        v2.push_back((r >> i) & 1);
    }
    int i = 0;
    int n1 = 0, n2 = 0, n3 = 0;
    while (i < 32 && v1[i] == v2[i])
    {
        n1 += (v1[i] & 1);
        n1 = n1 << 1;
        n2 += (v2[i] & 1);
        n2 = n2 << 1;
        i++;
    }
    n2 += 1;
    n2 = n2 << 1;
    i++;
    n1 = n1 << 1;
    while (i < 32)
    {
        n2 = n2 << 1;
        n1 += 1;
        n1 = n1 << 1;
        i++;
    }
    n1 = n1 >> 1;
    n2 = n2 >> 1;
    for (int i = l; i <= r; i++)
    {
        if (i != n1 && i != n2)
        {
            n3 = i;
            break;
        }
    }
    cout << n1 << " " << n2 << " " << n3 << endl;
}

int main()
{
    IOS tc
    {
        solve();
    }
}
Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 327 ms):
import java.util.*;
import java.io.*;

public class CF_1000_B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int test_cases = sc.nextInt();
        while (test_cases-- > 0) {
            int n = sc.nextInt();
            int l = sc.nextInt();
            int r = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = sc.nextInt();
            int[] arr1 = Arrays.copyOfRange(arr, 0, r);
            int[] arr2 = Arrays.copyOfRange(arr, l - 1, arr.length);
            Arrays.sort(arr1);
            Arrays.sort(arr2);
            long min1 = 0;
            long min2 = 0;
            for (int i = 0; i < r - l + 1; i++) {
                min1 += arr1[i];
                min2 += arr2[i];
            }
            sb.append(Math.min(min1, min2)).append("\n");
        }
        sc.close();
        System.out.println(sb.toString());
    }
}

Solution 2 (runtime: 359 ms):


import java.util.*;

import java.io.*;

public class Main {



	public static class cmp implements Comparator<Integer>{

		public int compare(Integer a,Integer b) {

			return a-b;

		}

	}

	public static BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));

	public static BufferedWriter wd=new BufferedWriter(new OutputStreamWriter(System.out));

	public static void solve()throws Exception{

		String dr[]=rd.readLine().split(" ");

		int n=Integer.parseInt(dr[0]),l=Integer.parseInt(dr[1]),r=Integer.parseInt(dr[2]);

		long a[]=new long [n+10],b[]=new long [n+10],c[]=new long [n+10];

		int t1=0,t2=0,t3=0;

		dr=rd.readLine().split(" ");

		long res=0;

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

			int x=Integer.parseInt(dr[i-1]);

			if(i<l) a[++t1]=x;

			if(i>r) {

				c[++t3]=x;

			//	res+=x;

			}

			if(i>=l&&i<=r){

				b[++t2]=x;

				res+=x;

			//	wd.write(res+" "+x+"\n");

			}

		}

	//	wd.write(res+"\n");

	//	for(int i=1;i<=t2;i++) wd.write(b[i]+"\n");

		Arrays.sort(a,1,1+t1);Arrays.sort(b,1,1+t2);Arrays.sort(c,1,1+t3);

	//	wd.write(res+"\n");

		//for(int i=1;i<=t3;i++) wd.write(c[i]+"\n");

		for(int i=1;i<=t1;i++) a[i]+=a[i-1];

		for(int i=1;i<=t2;i++) b[i]+=b[i-1];

		for(int i=1;i<=t3;i++) c[i]+=c[i-1];

		long ans=res;

		int k=Math.min(t1,t2);

		//wd.write(res+" "+ans+"\n");

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

			long tem=b[t2]-b[t2-i];

			//wd.write(tem+" "+i+"\n");

			//wd.write(tem-a[i]+"\n");

			ans=Math.min(ans, res-(tem-a[i]));

		}

		int k2=Math.min(t2, t3);

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

			long tem=b[t2]-b[t2-i];

		//wd.write(i+" "+tem+" "+(tem-c[i])+"\n");

			ans=Math.min(ans,res-(tem-c[i]));

		}

		wd.write(ans+"\n");

		//long res=0;

		//for(int i=1;i<=t3;i++) wd.write(c[i]+"\n");

	}

	public static void main(String[] args)throws Exception{

		int t=Integer.parseInt(rd.readLine());

		while(t-->0) {

			solve();

			wd.flush();

		}

	}



}
Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 593 ms):
import java.util.*;

public final class F1{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- != 0){
			int n = sc.nextInt();
			int l = sc.nextInt();
			int r = sc.nextInt();
			int[] arr = new int[n];
			int[] left = new int[r];
			int[] right = new int[n-l+1];
			for(int i = 0; i < arr.length; i++){
				arr[i] = sc.nextInt();
			}
			for(int i = 0; i < left.length; i++){
				left[i] = arr[i];
			}
			for(int i = 0; i < right.length; i++){
				right[i] = arr[l-1+i];
			}
			Arrays.sort(left);
			Arrays.sort(right);
			long sum1 = 0;
			long sum2 = 0; 
			for(int i = 0 ; i <= r - l; i++){
				sum1 += (long)left[i];
				sum2 += (long)right[i];
			}
			System.out.println(Math.min(sum1, sum2));
		}
	}		
}

Solution 2 (runtime: 499 ms):
import java.util.*;

public class Main
{
	public static void main(String[] args) {
		int t;
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		while(t > 0){
		    int n = sc.nextInt();
		    int l = sc.nextInt();
		    int r = sc.nextInt();
		    l--;
		    r--;
		    List<Integer> arr1 = new ArrayList<>();
		    List<Integer> arr2 = new ArrayList<>();
		    List<Integer> arr3 = new ArrayList<>();
		    for (int i = 0; i < l; i++){
		        arr1.add(sc.nextInt());
		    }
		    for (int i = l; i <= r; i++){
		        arr2.add(sc.nextInt());
		    }
		    for (int i = r + 1; i < n; i++){
		        arr3.add(sc.nextInt());
		    }
		    Collections.sort(arr1);
		    Collections.sort(arr2);
		    Collections.sort(arr3);
		  //  System.out.println(arr1);
		  //  System.out.println(arr2);
		  //  System.out.println(arr3);
		    long sum1 = 0, sum2 = 0;
		    int j = 0;
		    for (int i = arr2.size() - 1; i >= 0; i--){
		        int num1 = arr2.get(i);
		        if (j < arr1.size()){
		            num1 = Math.min(num1, arr1.get(j));
		            j++;
		        }
		        sum1 += num1;
		    }
		    j = 0;
		    for (int i = arr2.size() - 1; i >= 0; i--){
		        int num1 = arr2.get(i);
		        if (j < arr3.size()){
		            num1 = Math.min(num1, arr3.get(j));
		            j++;
		        }
		        sum2 += num1;
		    }
		    long ans = Math.min(sum1, sum2);
		    System.out.println(ans);
		    t--;
		}
	}
}
Problem: 2063B - Subsequence Update
Language: java

Solution 1 (runtime: 327 ms):
import java.util.*;
import java.io.*;

public class CF_1000_B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int test_cases = sc.nextInt();
        while (test_cases-- > 0) {
            int n = sc.nextInt();
            int l = sc.nextInt();
            int r = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = sc.nextInt();
            int[] arr1 = Arrays.copyOfRange(arr, 0, r);
            int[] arr2 = Arrays.copyOfRange(arr, l - 1, arr.length);
            Arrays.sort(arr1);
            Arrays.sort(arr2);
            long min1 = 0;
            long min2 = 0;
            for (int i = 0; i < r - l + 1; i++) {
                min1 += arr1[i];
                min2 += arr2[i];
            }
            sb.append(Math.min(min1, min2)).append("\n");
        }
        sc.close();
        System.out.println(sb.toString());
    }
}

Solution 2 (runtime: 499 ms):
import java.util.*;

public class Main
{
	public static void main(String[] args) {
		int t;
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		while(t > 0){
		    int n = sc.nextInt();
		    int l = sc.nextInt();
		    int r = sc.nextInt();
		    l--;
		    r--;
		    List<Integer> arr1 = new ArrayList<>();
		    List<Integer> arr2 = new ArrayList<>();
		    List<Integer> arr3 = new ArrayList<>();
		    for (int i = 0; i < l; i++){
		        arr1.add(sc.nextInt());
		    }
		    for (int i = l; i <= r; i++){
		        arr2.add(sc.nextInt());
		    }
		    for (int i = r + 1; i < n; i++){
		        arr3.add(sc.nextInt());
		    }
		    Collections.sort(arr1);
		    Collections.sort(arr2);
		    Collections.sort(arr3);
		  //  System.out.println(arr1);
		  //  System.out.println(arr2);
		  //  System.out.println(arr3);
		    long sum1 = 0, sum2 = 0;
		    int j = 0;
		    for (int i = arr2.size() - 1; i >= 0; i--){
		        int num1 = arr2.get(i);
		        if (j < arr1.size()){
		            num1 = Math.min(num1, arr1.get(j));
		            j++;
		        }
		        sum1 += num1;
		    }
		    j = 0;
		    for (int i = arr2.size() - 1; i >= 0; i--){
		        int num1 = arr2.get(i);
		        if (j < arr3.size()){
		            num1 = Math.min(num1, arr3.get(j));
		            j++;
		        }
		        sum2 += num1;
		    }
		    long ans = Math.min(sum1, sum2);
		    System.out.println(ans);
		    t--;
		}
	}
}
Problem: 2057C - Trip to the Olympiad
Language: java

Solution 1 (runtime: 234 ms):
import java.util.*;

public class C{
    public static boolean powerOf2(int n){
        if(n==1) return false;
        int a = 1;
        while(a<n) a = a*2;
        if(a==n) return true;
        return false;
    }
    public static void main(String...args){
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while(t>0){
            t--;

            int l = sc.nextInt();
            int r = sc.nextInt();

            int mid = 0;
            boolean smallerThanr = false, greaterThanl = false;

            int start = 30;
            int i = start;
            while(i>=0){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                if(lbit != rbit) break;
                mid += (lbit<<i);
                i--;
            }
            // System.out.println(mid);

            start = i;
            for(i=start; i>=0; i--){
                int lbit = (((1<<i)&l)>>i);
                int rbit = (((1<<i)&r)>>i);
                int mBit = 0;

                if(lbit==1 || rbit==1) mBit=0;
                else mBit = 1;
                mid += (mBit<<i);
            }
            // System.out.println(mid);

            
            i = start;
            while(i>=0 && mid <= l){
                int mBit = (((1<<i)&mid)>>i);
                // System.out.println(i + " " + mBit + " " + mid);
                if(mBit==0 && ((mid + (1<<i)) < r)){
                    mid += (1<<i);
                }
                i--;
            }
            System.out.println(l + " " + mid + " " + r);
        }
    }
}

Solution 2 (runtime: 281 ms):
import java.util.*;
public class ProblemC {

    static long mod = (long)(1e9+7);

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0) {
            int l = sc.nextInt();
            int r = sc.nextInt();
       
            int num = 0;
            for(int i =30;i>=0;i--) {
                if(((l&(1<<i))>0 && (r&(1<<i))>0)) {
                    num|=(1<<i);
                } else if(((l&(1<<i))==0 && (r&(1<<i))==0)) {
                    continue;
                } else {
                    num|=(1<<i);
                    break;
                }
            }

            int a = num;
            int b = num-1;
            int c = (l==b) ? r : l;
            System.out.println(a+" "+b+" "+c);
        }
    }
}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 234 ms):
import java.util.*;

public class DigitalStringMaximization{

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] result = new String[n];
		for(int i = 0; i < n; i++){
			result[i]= maximize(sc.next());
		}
		Arrays.stream(result).forEach(System.out::println);
	}
	
	private static String maximize(String str){
		char[] chars = str.toCharArray();
		int num;
		for(int i = 1; i < chars.length; i++){
			num = chars[i] - '0';
			for(int j = num; j >= 0; j--){
				if( i - j < 0) continue;
				if(chars[ i - j ] < (char)(chars[i] - j)){
					char temp = (char)(chars[i] - j);
					for(int k = i; k > i - j; k--){
						chars[k] = chars[k - 1];
					}
					chars[i - j] = temp;
					break;
				}
			}
		} 
		return new String(chars);
	}
	
	
}

/*
i = 1 51476
i = 2 53176
i = 3 53616 55316
I = 4 55316
i = 5 55351 55431
19


*/


Solution 2 (runtime: 234 ms):
import java.util.Scanner;

public class D_2050 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine();
        while (t-- > 0) {
            String s = sc.nextLine();
            char[] arr = s.toCharArray();
            int swapcount = 1;
            int ans = 0;
            while (swapcount != 0) {
                int sw = 0;
                for (int i = 1; i < arr.length; i++) {
                    char a = (char) (arr[i] - 1);
                    char b = arr[i - 1];
                    if ((arr[i] != '1' || arr[i] != '0') && a - b > 0) {
                        if (arr[i] > arr[i - 1]) {
                            swap(i - 1, i, arr);
                            sw++;
                        }
                    }
                }

                if (sw == 0) {
                    break;
                }
            }
            System.out.println(new String(arr));
        }
    }

    public static void swap(int i, int j, char[] arr) {
        arr[j] = (char) (arr[j] - 1);
        char t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 483 ms):
import java.util.*;

public class question {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while (t-- > 0) {
            int n = sc.nextInt();

            int[] arr = new int[n];

            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }

            int help = 0 ;

            for(int i = 0 ; i < n-1 ; i++){
                int a = arr[i];
                int b = arr[i+1];

                if(a > b){
                    System.out.println("NO");
                    help++;
                    break;
                }else{

                    arr[i+1] = arr[i+1] - arr[i];
                    arr[i] = 0 ;
                    //System.out.println(arr[i+1]);
                }
            }


            if(help == 0){
                System.out.println("YES");
            }

        }
    }
}


Solution 2 (runtime: 499 ms):
import java.util.*;



public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt(); // Number of test cases

        

        while (t-- > 0) {

            int n = sc.nextInt(); // Length of sequence

            int[] a = new int[n];

            

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

                a[i] = sc.nextInt();

            }



            // Simulating the operation

            boolean possible = true;

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

                int minValue = Math.min(a[i], a[i + 1]);

                a[i] -= minValue;

                a[i + 1] -= minValue;

            }



            // Check if the array is now non-decreasing

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

                if (a[i] > a[i + 1]) {

                    possible = false;

                    break;

                }

            }



            System.out.println(possible ? "YES" : "NO");

        }



        sc.close();

    }

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 311 ms):
import java.io.*;
import java.util.*;

public class d {
	
	static BufferedReader br;
	static StringTokenizer st;
	static StringBuilder sb = new StringBuilder("");
	
	public static void main (String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		st = new StringTokenizer(br.readLine());
		
		int t = Integer.parseInt(st.nextToken());
		
		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(br.readLine());
			solve();
		}
		
		br.close();
		System.out.print(sb.toString());
	} 
 
	
	static void solve() throws IOException {
		int n = Integer.parseInt(st.nextToken());
		int[] A = new int[n+1];
		
		st = new StringTokenizer(br.readLine());
		
		for (int i = 1; i <= n; i++) {
			A[i] = Integer.parseInt(st.nextToken());
		}
		
		for (int i = 1; i <= n-1; i++) {
			int m = Math.min(A[i], A[i+1]);
			A[i] -= m;
			A[i+1] -= m;
		}
		
		
		for (int i = 1; i <= n-1; i++) {
			if (A[i] > A[i+1]) {
				sb.append("No\n");
				return;
			}
		}
		
		sb.append("Yes\n");
		
	}
	
	
	
}

Solution 2 (runtime: 639 ms):
import java.util.Scanner;



public class Main {

    static  boolean check(int[] arr){

        for (int i = 0; i < arr.length-1; i++) {

            if (arr[i]>arr[i+1]) return  false;

        }

        return true;

    }

    public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);

        int t = sc.nextInt();

        while(t-->0) {

            int n = sc.nextInt();

            int[] a =new int[n];

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

                a[i]= sc.nextInt();

            }

            boolean flag=false;

            int i =0;

            while (i< n-1){

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

                a[i]=a[i]-min;

                a[i+1]=a[i+1]-min;

                i++;

            }

            if (check(a)) System.out.println("YES");

            else System.out.println("NO");

        }

    }

}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 234 ms):
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.StringTokenizer;



public class round991 {

static class Pair implements Comparable<Pair>{

int a;

long b;

String str;



Pair(int a,long b,String str){

this.a=a;

this.b=b;

this.str=str;

}



public int compareTo(Pair o) {

return Integer.compare(this.a, o.a); // Ascending order by a

}

//

//            public int compareTo(Pair o){

//                return Long.compare(this.b,o.b);

//            }

//

//            public int compareTo(Pair other) {

//                return this.str.compareTo(other.str); // Ascending order by str

//            }

//

}

static class FastReader {

BufferedReader br;

StringTokenizer st;



public FastReader() {

br = new BufferedReader(

new InputStreamReader(System.in));

}



String next() {

while (st == null || !st.hasMoreElements()) {

try {

st = new StringTokenizer(br.readLine());

} catch (IOException e) {

e.printStackTrace();

}

}

return st.nextToken();

}



int nextInt() {

return Integer.parseInt(next());

}



long nextLong() {

return Long.parseLong(next());

}



double nextDouble() {

return Double.parseDouble(next());

}



String nextLine() {

String str = "";

try {

if (st.hasMoreTokens()) {

str = st.nextToken("\n");

} else {

str = br.readLine();

}

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

static ArrayList<Integer>[] gr;

public static void main(String[] args) {

FastReader sc = new FastReader();

PrintWriter out=new PrintWriter(System.out);

int t = sc.nextInt();

while (t != 0) {

t--;

String str=sc.next();

int[] arr=new int[str.length()];

for (int i = 0; i < arr.length; i++) {

arr[i]=str.charAt(i)-'0';

}

StringBuilder ans=new StringBuilder("");

for (int i = 0; i < arr.length; i++) {

int dig=arr[i];

int j=i-1;

while (j>=0 && arr[j]<dig-1){

arr[j+1]=arr[j];

--dig;

arr[j]=dig;

--j;

}

//                out.println(st);

}

//            out.println(Arrays.toString(arr));

for (int num:arr) {

ans.append(""+num);

}

out.println(ans);

}

out.flush();

}

}



Solution 2 (runtime: 296 ms):


import java.util.*;

import java.io.*;



public class DigitalStringMax {



public static void main(String args[]) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

PrintWriter pw = new PrintWriter(System.out);



int tt = Integer.parseInt(br.readLine());

while(tt-- > 0) {



char ch[] = br.readLine().toCharArray();



int n = ch.length;

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

int max = -1;

int ind = -1;

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



if(max < (ch[j] - (j - i))){

max = (ch[j] - (j - i));

ind = j;

}



}



ch[ind] = (char)(ch[ind]  - (ind - i ));

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

char temp = ch[j];



ch[j] = ch[j-1];

ch[j-1] = temp;



}

}



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

pw.print(ch[i]);

}

pw.println();



}

pw.flush();

}

}