Help me in solving SHORTSPELL problem

My issue

what is wrong in this code

My code

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static String smallestSpell(String spell) {
        int minIdx = 0;
        int x=1;
        for (int i = 1; i < spell.length(); i++) {
            if (spell.charAt(i) < spell.charAt(i-1)) {
                minIdx =i-1;
                break;
            }
            x++;
            if(x==spell.length()-1){
                 return spell.substring(0, x) ;
            }
        }
        return spell.substring(0, minIdx) + spell.substring(minIdx + 1);
    }
    
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
	 Scanner scanner = new Scanner(System.in);

        
int T = scanner.nextInt();
        for (int t = 0; t < T; t++) {
            int N = scanner.nextInt();
            String spell= scanner.next();
            System.out.println(smallestSpell(spell));
        }
	
}
}

Problem Link: Spell Shortening Practice Coding Problem - CodeChef

@deepak_5047
here plzz refer my code

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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    string s;
	    cin>>s;
	    int ch=0;
	    string ans;
	    for(int i=1;i<n;i++)
	    {
	        if(s[i-1]>s[i]&&!ch)
	        {
	            ch=1;
	        }
	        else
	        ans+=s[i-1];
	    }
	    if(ch)
	    ans+=s[n-1];
	    cout<<ans<<endl;
	}

}