Fixed testcases and sorting

still problem with integer overflow