while S is not empty do pop e from S, enqueue e into Q end while while Q is not empty do dequeue e from Q, push e onto S end while