import java.util.*;
public class Solution {
   
    public static void main (String[] args)
    {
            //Test case 1
            List<List<Object>> intervals = new ArrayList<>();
           
            List<Object> entries = new ArrayList<>();
            
            /*
            System.out.println("{1,4}, {2,6}, {7,9}");
            entries.add("[1, 4]");
            entries.add("[2, 6]");
            entries.add("[7, 9]");
           */
           
           /*
            //PASS  - all overlap except last entry
            System.out.println("{1,3}, {2,8}, {6,10}, {11,14}");
            entries.add("[1, 3]");
            entries.add("[2, 8]");
            entries.add("[6, 10]");
            entries.add("[11, 14]");
           */
           
           
           // ALL OVERLAP NOW - PASS 
           System.out.println("{1,3}, {2,8}, {6,10}, {9,14}");
            entries.add("[1, 3]");
            entries.add("[2, 8]");
            entries.add("[12, 15]");
            entries.add("[9, 19]");
            entries.add("[13, 33]");
           
           
           /*
           // OVERLAP NOW (less objects) - PASS 
           System.out.println("{1,3}, {5,8}");
            entries.add("[1, 3]");
            entries.add("[5, 8]");
           */
           
           /*
           //FAILS - one overlap   it gives [1,17],  [20,25]
           System.out.println("{1,5}, {4,9}, {12,17}, {20,25}");
            entries.add("[1, 5]");
            entries.add("[4, 9]");
            entries.add("[12, 17]");
            entries.add("[20, 25]");
            */
            
            /*
           //FAILS - no overlap. it adds first two then crashes...  
           System.out.println("{1,5}, {6,9}, {12,17}, {20,25}");
            entries.add("[1, 5]");
            entries.add("[6, 9]");
            entries.add("[12, 17]");
            entries.add("[20, 25]");
           */
           
            /*
           //FAILS - no overlap first two then overlap then overlap
           //it adds first two into innerList then crashes
           System.out.println("{1,5}, {6,9}, {8,17}, {16,25}");
            entries.add("[1, 5]");
            entries.add("[6, 9]");
            entries.add("[8, 17]");
            entries.add("[16, 25]");
           */
           
            intervals.add(entries);
           
            System.out.println("THIS IS THE OUTER LIST: "    + mergeIntervals(intervals));
           
           
        //mergeIntervals(new ArrayList<>(Arrays.asList(["[1,4]", "[4,5]"])));
    }
   
    public static List<List<Object>> mergeIntervals(List<List<Object>> intervals) {

        List<List<Object>> outerList = new ArrayList<>();

        List<Object> innerList = new ArrayList<>();  //this is the list inside list to satisfy return type
        
        String endNewInterval="";
        
        int counter=0;

        String odd="";
        String even="";

        int endFirstRange;
        int startSecondRange;

        String evenSubStringStartInterval="";
        String oddSubStringEndInteveral="";

        String oddSubStringStartInterval="";
        String evenSubStringEndInterval="";
       
        String newInterval="";
        int objectNumber=0;
       
        boolean overlap=false;
       
       boolean newPairRangesMerged=false;
       int numberObjects=0;
       
        //first need to understand that return type is List < List<Integer> >
        //I am not sure if System.out.println() will print this since I am typically not used to having a collection within a collection..
        //Since we are returning variable of this datatype, I presume the test environment will take care of this scenario..
        // It will only become an issue once I configure the driver class with main method

        //Also require an iterator through the list

        Iterator<List<Object>> it = intervals.iterator();

        //since the data inside mergeIntervals is a List, I think the hasNext() might not be applicable
        //since this would refer to a String....
       
        //need to iterate through the List items differently
       
        System.out.println("REACH HERE");
       
        for (List <Object> tt: intervals)
        {
            for (Object s:tt)
            {
                numberObjects++;
            }
            
            for (Object s:tt)  //process each Object in the intervals   {X,Y}, {X,Z}......
            {
                objectNumber++;
                System.out.println("\nThis is the object: " + s + "(Object number: " + objectNumber+")");
                Object temp = s;
               
                counter++;
                System.out.println("This is counter: " + counter);
               
                 if (counter%2==1)
                {

            odd = String.valueOf(temp);
            oddSubStringEndInteveral=odd.substring((odd.indexOf(" ")+1),odd.indexOf("]"));
           
            oddSubStringStartInterval = odd.substring(1,odd.indexOf(","));

            System.out.println("RANGE1 start:" + oddSubStringStartInterval);
            System.out.println("RANGE1 end:" + oddSubStringEndInteveral);
           

                }
               
               
                 if (counter%2==0)
            {
                even = String.valueOf(temp);

                evenSubStringStartInterval = even.substring(1,even.indexOf(","));
                evenSubStringEndInterval = even.substring((even.indexOf(" ")+1),even.indexOf("]"));

                System.out.println("RANGE2 start:" + evenSubStringStartInterval);
                System.out.println("RANGE2 end:" + evenSubStringEndInterval);
            }
           
            //this ensures odd and even process starts again
            System.out.println("even string: " + even);
            System.out.println("odd string: " + odd);
            System.out.println(oddSubStringEndInteveral);
            System.out.println(evenSubStringStartInterval);
           
           
             if (odd!="" && even!="" && counter==2)
            {
                System.out.println("UUUUUUUU");
                if (Integer.valueOf(oddSubStringEndInteveral)>Integer.valueOf(evenSubStringStartInterval))
                {
                    System.out.println("OVERLAP");
                   
                   System.out.println("Object number: " + objectNumber);
                   System.out.println("Objects in intervals: " + numberObjects);
                   
                   if (numberObjects==2)
                   {
                       newInterval = "["+oddSubStringStartInterval+", "+evenSubStringEndInterval+"]";
                        innerList.add(newInterval);
                        break;
                       
                   }
                   
                   
                   if (numberObjects==objectNumber)
                   {
                       //innerList.add(even);
                       System.out.println("BREAKING OUT");
                       System.out.println("this is counter: " + counter);
                       counter=1;
                      
                   }
                   
                   
                   System.out.println(newInterval);
                   
                   if (counter==2)
                   {
                    newInterval = "["+oddSubStringStartInterval+", "+evenSubStringEndInterval+"]";
                    innerList.add(newInterval);
                    System.out.println("**************************************************New interval into outer List ADDED :" + newInterval);
                    overlap=true;
                   
                    newPairRangesMerged=true;
                    //overlap
                   // {1,3}, {2,8}, {6,10}, {11,14}
                   
                    //we know the next iteration will be odd {6,10}
                    //but this has to be compared against  even {2,8} - note this has already been processed from the for each loop
                   
                    //we know that existing entry in the outerList [1,8] => [1,10]
                   
                    //so we have to compare the last entry in the outerList [1,8] with next innerlist item [6,10]
                   
                    //outerList.add(newInterval);
                   }
                }
               
                else
                {
                    System.out.println("HERE1");

                    //only required to add odd since the next range (even) might overlap with next odd
                   
                     //ODD
                    //{1,3}, {2,8}, {6,10}, {11,14}

                    //odd;
                    
                    System.out.println("---------------------------DATA CHECK");
                    System.out.println(endNewInterval);
                    System.out.println(oddSubStringEndInteveral);
                    System.out.println("---------------------------");
                    
                    System.out.println("111INNER list right now: " + innerList);
                    System.out.println("IS THERE OVERLAP: " + overlap);
                    
                    if (!overlap)
                    {
                        System.out.println(endNewInterval);
                    System.out.println(oddSubStringEndInteveral);
                        
                         if(endNewInterval.equals(oddSubStringEndInteveral))
                         {
                            System.out.println("789**********************************ADDED: " + even);
                            System.out.println("INNER list right now: " + innerList);
                            innerList.add(even);          
                            
                         }
                         
                         else
                         {
                             System.out.println("123**********************************ADDED: " + odd);
                             System.out.println("125**********************************ADDED: " + even);
                             innerList.add(odd);
                            innerList.add(even);  
                            System.out.println("INNER list right now: " + innerList);
                         }
                        
                        //System.out.println("NO OVERLAP!");
                    //System.out.println("**********************************ADDED: " + odd);
                    
                    
                    
                    
                    
                    }
                    else
                    {
                        System.out.println("OVERLAP!!*******************");
                        
                    //innerList.add(odd);
                    //innerList.add(even);
                        
                    }
                    
                    
                }
               
            }
               
                //need also a control on here
                //since if the flag is set above, it will enter in here again
                //this loop designed for check of three or more multiple overlap occurrences
                //since this accesses the innerList directly to modify it
                //we know that it shouldnt also go in when it has picked out the first item
                //from intervals list!
               
               System.out.println("CHANCE BACK HERE");
               
                if (!overlap && counter==1 && objectNumber>1)
                {
                    //it would perform for each an additional time...
                    //it would then compare against the outerList last item
                    //this should always be the newInterval since this variable has been set when the flag was set
                   
                    //this seems to be more straightforward...
                    // if it does not overlap, it will set the flag to false....
                   
                   System.out.println("13this is current new interval:  " + newInterval);
                   System.out.println(innerList);
                   
                   
                   if (newInterval=="")
                   {
                       newInterval = String.valueOf(innerList.get(innerList.size()-1));
                       
                       System.out.println("This has been assigned to new interval: " + newInterval);
                       
                       //break;
                   }
                   
                   
                   if (objectNumber==numberObjects)
                        {
                            if (counter==1)
                            {
                            innerList.add(odd);
                            System.out.println("***11CHANGE*********************ADDED INTO INNERLIST: " + odd);
                            break;
                            }
                            else
                            {
                            innerList.add(even);
                            System.out.println("***12CHANGE*********************ADDED INTO INNERLIST: " + even);
                            break;
                                
                            }
                        }
                   
                    endNewInterval = newInterval.substring(newInterval.indexOf(" ")+1,newInterval.indexOf("]"));
                    System.out.println("REVISED ENDNEWINTERVAL: " + endNewInterval);
                    
                    System.out.println("----CURRENTLY IN TEMP: " + temp);
                   
                    String subStringStartInterval = String.valueOf(temp).substring(1,String.valueOf(temp).indexOf(","));
                    System.out.println("REVISED startinterval: " + subStringStartInterval);
                   
                    if (Integer.valueOf(endNewInterval)>Integer.valueOf(subStringStartInterval))
                    {
                        System.out.println("************OVERLAP: ");
                        System.out.println(even);
                        System.out.println(odd);
                       
                           
                        //we would need to overwrite the last item in the innerList with new range
                        //list is zero index based
                       
                        //this is the intervals
                        //{1,3}, {2,8}, {6,10}, {11,14}
                       
                        //this is the innerlist. need to remember might be other items before it.
                        //{X,X},{1,8}
                        //this entry will be removed, so need to keep a copy of it since '1' is of interest.
                        // 6 is currently stored in oddSubStringStartInterval
                        //However we require 1.
                        //so need to think about how to pull the associated
                        //start of the merging range..
                        //we know some variable would have been reset
                        //alternatively we know that if we perform 
                        //innerList.get(innerList.size()-1), we can perform substring to get start of the range
                        
                        Object lastItemInnerList = innerList.get(innerList.size()-1);
                        String lastItemInnerListString = String.valueOf(lastItemInnerList);
                        
                        String startIntervalLastItemInnerList = lastItemInnerListString.substring(1,lastItemInnerListString.indexOf(","));
                        System.out.println("ultimate start of range: " + startIntervalLastItemInnerList);
                        
                        //we need to remember the merging will initiate from pairs and this variable will remain valid
                       System.out.println("*************************REMOVED:  " + innerList.get(innerList.size()-1));
                       innerList.remove(innerList.size()-1);
                       
                        if (numberObjects==objectNumber)
                   {
                       //we need to get the endinterval from the even String
                       endNewInterval = even.substring((even.indexOf(" ")+1),even.indexOf("]"));
                       
                   }
                   else
                   {
                        endNewInterval = odd.substring((odd.indexOf(" ")+1),odd.indexOf("]"));
                        
                   }   
                   
                   newInterval = "["+startIntervalLastItemInnerList+", " + endNewInterval +"]";
                   innerList.add(newInterval);     //{1,10}
                        System.out.println("**********************************************NEW INTERVAL SET ADDED => " + newInterval);
                       
                        //now need to think from the perspective of it entering up to here again....
                       
                        //taking this to be the new data.
                        //it would process  {9,14}
                        //{1,3}, {2,8}, {6,10}, {9,14}
                       
                        //it would perform this:
                        //String endNewInterval = newInterval.substring(newInterval.indexOf(" ")+1,even.indexOf("]"));
                        //It would obtain 10  from {1,10}
                       
                        //it would perform String subStringStartInterval = temp.substring(1,even.indexOf(","));
                        //we know temp is {9,14}
                        //it would obtain 9 and identify overlap with 10.
                        //it would then remove last item in the innerList and store
                       
                        //newInterval = "["+oddSubStringStartInterval+"," + " " + endNewInterval;
                        //since it is a continuous overlap of ranges,  oddSubStringStartInterval = 1 is still valid
                       
                        //endNewInterval {14} is obtained via:
                        //String endNewInterval = newInterval.substring(newInterval.indexOf(" ")+1,even.indexOf("]"));
                        //and we know the newInterval is configured to {1,14}
                    }
                    else
                    {
                        if (objectNumber==numberObjects)
                        {
                            System.out.println(innerList.add(odd));
                            System.out.println("*********************ADDED INTO INNERLIST: " + odd);
                        }
                    }
                   
                }
               
                 else
                {
                    //we can set this status so that it stays clear of the associated if loop...
                    overlap=false;
                    //innerList.add(even);
                   
                    //it requires more thought again what code will do on the next execution.....
                   
                    //lets assume
                    //{1,3},{4,6},{5,7}, {6,9}
                    //it would have finished executing {1,3},{4,6}
                    //and commencing {5,7}
                    //but there is a statement to start process when odd and even are populated.
                    //but we still need to focus on {4,6},{5,7}
                   
                    //so the option is to set the counter to 1
                    //counter would then become  2  when it processes  {5,7}  but it would store this in even
                    //this would overwrite the existing even {4,6}
                   
                    //we need to make the counter =0
                    //it will populate String odd
                    //we know the overlap process is part of pairs
                    //the String even is the comparing range.
                   
                   
                    //only reset the counter once the counter has reached 2, otherwise all entries from the intervals will reach here since there will be no
                    //overlap after retrieving the first range.. And then it will execute again and simply set String odd and overwrite it....
                   
                    if (counter==2)
                    {
                        counter=0;
                        System.out.println("counter being reset");
                        
                        
                        
                       
                        //it does not matter if we clear the odd, but best it is actioned...
                        //BUT Definitely not clear the even
                        odd="";
                       
                    }
                   
                }
               
            }
        }
       
        //the final step would be to add the innerList into the outerList
       
        System.out.println("checking");
        //System.out.println(innerList.get(0));
        //System.out.println(innerList.get(1));
        //System.out.println(innerList.get(2));
       
        outerList.add(innerList);
       
        return outerList;
    }

}