generalize pair-locations by distinguishing before loop and in loop
getting desired output