SP26706 SORTMAC - Sort Machine

Description

We have a sorting machine that works on a list of distinct numbers. This machine only has two instructions, named MOVEBACK and MOVEFRONT. Each instruction takes one element of the list as a parameter and removes that element from the list. MOVEBACK will then append that element to the end of the remaining list, while MOVEFRONT will insert it at the beginning. For example, the sequence {8,12,25,7,15,19} can be sorted in ascending order using 2 instructions: - MOVEFRONT 7, to get {7,8,12,25,15,19} - MOVEBACK 25, to get {7,8,12,15,19,25} You will be given a list of distinct numbers. Compute the minimum number of instructions required to sort the list in ascending order.

Input Format

Input starts with an integer T ( Each case starts with an integer N (1

Output Format

For each case, print one line containing the answer to the problem.