Where am i wrong? even my code is correct :(

only the order is different but it is correct , like dependencies are resolved correctly

how do we sort them acc to input given in the last ??

only thats the mistake

store the original inputs in an array
according to their index
now for all the values already visited in graph u need not do anything
those, which are not visited, print them in order of the input from the original array
also in some cases, ur answer might be right but it will still not pass
since there are multiple answers to this questions which are all correct so u need to hardcode a few test cases
u can open the question in some other contest and hardcode that particular case