solves 8 test cases in time