#Perl 5, 68 bytes

65 bytes plus three for the flags: `perl -lpF script.pl`

    $"='';@a="@F@F"=~/#(\d+)/;{unshift@F,pop@F;++$i!=$1&&redo}$_="@F"

I stole the `concat(input,input)` trick from other answers here.