# Permutation Minimization by Deque Solution Codeforces

A permutation $$p$$$of size $$n$$$ is given. A permutation of size $$n$$$is an array of size $$n$$$ in which each integer from $$1$$$to $$n$$$ occurs exactly once. For example, $$[1, 4, 3, 2]$$$and $$[4, 2, 1, 3]$$$ are correct permutations while $$[1, 2, 4]$$$and $$[1, 2, 2]$$$ are not.

## Permutation Minimization by Deque Solution Codeforces

Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $$[1, 5, 2]$$$currently in the deque, adding an element $$4$$$ to the beginning will produce the sequence $$[\color{red}{4}, 1, 5, 2]$$$, and adding same element to the end will produce $$[1, 5, 2, \color{red}{4}]$$$.

The elements of the permutation are sequentially added to the initially empty deque, starting with $$p_1$$$and finishing with $$p_n$$$. Before adding each element to the deque, you may choose whether to add it to the beginning or the end.

## Permutation Minimization by Deque Solution Codeforces

Input

The first line contains an integer $$t$$$($$1 \leq t \leq 1000$$$) — the number of test cases.