I am getting time limit exceeded

how to get output in desired manner for this question

while(d!=-1){
why you have put this condition
in question size of array is given there is no -1

also you have not taken no of testcase
first read the question’s input format

plz read question carefully