Let's analyze the second example.

Initially, $p = [5, 3, 1, 4, 2]$. 

The first query is of type $3$, meaning take the inverse. The permutation after this query becomes $[3, 5, 2, 4, 1]$. The \textit{cost} of this permutation is $3^1 + 5^2 + 2^3 + 4^4 + 1^5 = 3 + 25 + 8 + 256 + 1 = 293$.

\

The second query is of type $3$, meaning take the inverse. The permutation after this query becomes $[5, 3, 1, 4, 2]$. The \textit{cost} of this permutation is $5^1 + 3^2 + 1^3 + 4^4 + 2^5 = 5 + 9 + 1 + 256 + 32 = 303$.

\

The third query is of type $1$, meaning reverse. The permutation after this query becomes $[2, 4, 1, 3, 5]$. The \textit{cost} of this permutation is $2^1 + 4^2 + 1^3 + 3^4 + 5^5 = 3225$.

\

The fourth query is of type $2$, meaning invert. The permutation after this query becomes $[4, 2, 5, 3, 1]$. The \textit{cost} of this permutation is $4^1 + 2^2 + 5^3 + 3^4 + 1^5 = 215$.

\

The fifth query is of type $3$, meaning take the inverse. The permutation after this query becomes $[5, 2, 4, 1, 3]$. The \textit{cost} of this permutation is $5^1 + 2^2 + 4^3 + 1^4 + 3^5 = 317$.

\

The last query is of type $1$, meaning reverse. The permutation after this query becomes $[3, 1, 4, 2, 5]$. The \textit{cost} of this permutation is $3^1 + 1^2 + 4^3 + 2^4 + 5^5 = 3209$.