GONESORT - G-One Sort

no tags 

After Killing RaOne G-One had nothing to do, so he started reading books and became an avid book reader.

To avoid purchasing books he started working in a library.

Every evening he had to arrange the books on the shelf in increasing order of their serial number.

Every book in the library is numbered.

..

G-One found an ingenious way of arranging the books.  He can remove any book from the shelf and put it either at the beginning or at the end of the shelf.

For example if the books are arranged in the order below:

    2  3  1  7  4  5  6

he can make it sorted by removing '1' and placing it at the beginning and then removing '7' and placing it in the end.

Since the book shelf can be very big and can have a large number of books, he needs your help to tell him the minimum remove and place operations he needs to do.

Can you help him?

Input

1st line of the input contains number 't' denoting the number of shelves in the library. 2*t lines follow this

1st line of each test case will have single number 'b' - denoting number of books on the shelf.

2nd line will contain b numbers, each bi denoting the serial number of the book.

Output

For each test case output a single integer denoting the minimum number of remove and place operations needed to arrange the shelf.

Example

Input:
3
7
2 3 1 7 4 5 6
5
1 2 3 4 5
6
6 5 4 3 2 1

Output:
2
0
5

NOTE All the values will be in the range [0, 107], number of test cases ≤ 100.


hide comments
Devil D: 2012-04-07 03:42:32

@ MITCH - All serial numbers are different ...

Mitch Schwartz: 2012-04-07 03:42:32

Numbers cannot be repeated, right? So this

4
1 1 1 4

is not a valid test case?

I think if I give wrong answers they must all be too high, since the algorithm could be easily modified to output a sequence of moves. I'll look again for an error.

------------------------------
Yes not a valid test case...

------------------------------
(from Mitch) All right, well thanks for the replies. I'll set it aside for now. I'll assume from your silence about the other point that it is true -- my answers are all greater than or equal to the true answers. (If not, then you may suggest a test case, for which I can produce steps corresponding to the answer I gave.)

Last edit: 2012-04-06 06:35:22
Devil D: 2012-04-07 03:42:32

@Mitch - You are getting wrong answers for n>8 ... Test cases are correct

Mitch Schwartz: 2012-04-07 03:42:32

Are you sure the test data is correct? I have tested my algorithm against a brute forcer for all permutations with 1<=n<=7 and many random cases with n=8.. reviewed the logic.. I can't find any problem.

Last edit: 2012-04-05 22:24:04
Devil D: 2012-04-07 03:42:32


All the values will be in the range [0, 10^7] exclusive

Last edit: 2012-04-05 11:16:01
Radhakrishnan Venkataramani: 2012-04-07 03:42:32

what is limit for b ? the total number of books in a shelf ?


Added by:Devil D
Date:2012-04-05
Time limit:0.100s-1s
Source limit:20000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own