TOPOLOGICAL ORDER

hi codechef community …someone plz help me i n this question

after taking out the topological order how can i make the order lexicographically least greater than array B ??