Brackets All Over

My code is producing correct output for smaller test cases but not working for bigger testcases.