I'm trying to solve this task backwards, after reading input data I calculate where is each number going to be. But I have a problem doing queries. If I insert a number at his final position i have to change the upcoming queries. I'm sure that I'm doing this wrong, but I don't have any ideas how to make it good. Any help would be welcome.