Each row of legionary was designed to fight for 10 - 15 minutes, until the soldiers became tired, whereupon the captain (500 men commander) would blow a wooden whistle, and the legionnaires would take one step to their right, while their comrades immediately behind them would step forward to fill their previously occupied space, and immediately start engaging the enemy.
While at the same time, the tired troops would take a step back, and with their comrades protecting their backs, the troops would turn back and be able to safely retreat to the back, letting the whole cycle continue.
And in this way, entire companies and even the entire legion can be seamlessly switched without having to disengage with the enemy, enabling an army to fight for hours without tiring, with individual soldiers maybe fighting a total of 10-15 minutes per battle.