ORDERS: getting WA after WA

Problem link: https://www.codechef.com/problems/ORDERS

My code works with the given test data, but I keep getting WAs on submission. I even went to the extent of removing extra spaces and new lines, which I don’t think is necessary (don’t know, new here). Here’s my code:

My code: https://pastebin.com/46cpMbt2

Please point me in the right direction.

The test data given only has permutations that are self-inverse. Here’s some more complex test data:

input

1
14
0 1 1 2 3 1 0 0 0 3 3 8 2 2

output

7 1 5 3 2 6 10 13 14 8 9 4 11 12