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
Sourabh Jain: 2012-10-21 12:07:43

bi contains distinct values and 1<=bi<=N

StupidGuy: 2012-08-12 04:43:05

How is ans for 10 22 9 33 21 50 41 60 80 is 8?
-------------------------------------------------
10 22 9 33 21 50 41 60 80
41 10 22 9 33 21 50 60 80 - 1
33 41 10 22 9 21 50 60 80 - 2
22 33 41 10 9 21 50 60 80 - 3
21 22 33 41 10 9 50 60 80 - 4
10 21 22 33 41 9 50 60 80 - 5
9 10 21 22 33 41 50 60 80 - 6
-------------------------------------------------

It should be 6. Are wrong answers in order?
@data - I didn't see your comment there, you are right.

Last edit: 2012-08-12 04:44:02
data: 2012-08-07 15:57:01

@:P if m correct answer =6
by putting book no 41,33,22,21,10,9 in begining respectively
pls correct me if wrng

Surya: 2012-07-26 13:29:26

@Mukul: For 10, 22, 9, 33, 21, 50, 41, 60, 80
Answer is 8.

Be careful that he can only place them at the ends and cant insert between elements. Costed me 4 WAs! :(

Mukul: 2012-07-21 20:02:00

what will be answer for...
10, 22, 9, 33, 21, 50, 41, 60, 80 ??

Mukul: 2012-07-21 17:32:19

O(n^2) is enough :)

Last edit: 2012-07-21 17:56:43
npsabari: 2012-07-19 18:05:10

O(n^2) takes 0.02 sec!

Last edit: 2012-07-19 18:14:01
Himanshu Srivastava: 2012-06-28 05:01:22

don't confuse with lcs ! :P

Last edit: 2012-12-12 20:16:07
Rajesh Kumar: 2012-06-27 21:27:48

Similar Problem
www.spoj.pl/problems/KNJIGE

abhiranjan: 2012-05-28 14:02:42

Let's be more clear about range.

0 <= t <= 100, test cases

0 <= b <= 250, number of books

0 <= bi <= 107 , number on books


UPD: affirmed by assertions.

Last edit: 2012-05-28 14:06:01

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