Smallest kmp (SKMP) August Long Challenge 2020

Can u please give me any suggestions to improve execution speed of my code as I have passed test cases of first subset get getting tle in next.I have typed this code on mobile so there may be issues in spaces.I have added necessary comments plz have a look-

 import java.util.*;
 import java.io.*;
 class hshs {
      public static void main(String[] args) throws IOException {

          BufferedReader no= new BufferedReader(new 
          InputStreamReader(System.in));
          int t=Integer.parseInt(no.readLine());
          for(int o=0;o<t;o++){
             StringBuilder sb1=new StringBuilder();
             String a=no.readLine();
             String b=no.readLine();
             int d=0;
             int q=0;
            char chararr[]=a.toCharArray();
            Arrays.sort(chararr);
            String c=new String(chararr);
            String f=c;
            for(int j=0;j<b.length();j++){
                StringBuilder sb=new StringBuilder();
               sb.append(c);
               for(int k=0;k<c.length();k++){
                      if(b.charAt(j)==c.charAt(k)){
                           sb.deleteCharAt(k);
                           break;
                      }
               }
              c=sb.toString();
           }

//Upto here a reduced string of String a is made which doesnt consist characters of String b.This reduced String is now named c

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

  //if any element in c matches with first element of b

              int h=c.length()-1;
              if(c.charAt(i)==b.charAt(0)){
                   int r=i;
   //Now check last element in c upto which it is same in b

              while(c.charAt(i)==b.charAt(0)&&i!=h){
                      i++;
              }

    //Now if it goes till end then to check with respect to c increase its value by 1 as this time its value will not increase to next level as loop stops due to reaching end
 
           if(i==h&&c.charAt(i)==b.charAt(0)){
                i++;
           } 

    //check where to put before or after repeating elements
    if((c.substring(0,i)+b).compareTo(c.substring(0,r)+b+c.substring(r,i))<0){
      sb1.append(c.substring(0,i));
      sb1.append(b);
      sb1.append(c.substring(i,h+1));
      break;
    }
    else{
      sb1.append(c.substring(0,r));
      sb1.append(b);
      sb1.append(c.substring(r,h+1));
      break;
    }
  }

  //Check if we get element greater than first element of b

  else if(c.charAt(i)>b.charAt(0)){
    sb1.append(c.substring(0,i));
    sb1.append(b);
    sb1.append(c.substring(i,h+1));
    break;
  }

  //if it goes till end without getting same or less due to elimination of those terms from String b e.g. if a=aappstk and b=stk then c=aapp due to elimination and now append it last to it

  else if(i==h){
    sb1.append(c);
    sb1.append(b);
    break;
  }
}
System.out.println(sb1.toString());

}
}
}

I know it’s too cumbersome but is there any way or I have to check other methods.Thanks in advance

Your code is running in o(n^2) and constraints for both string is 10^5.
you have to come up with optimize solution.