Under construction

Still being built. Things may change

Delete and Append Sort

Time limit2sMemory limit512 MB

Summary
Each operation moves one element to the end. Find the minimum number of such moves needed to sort the array.
Level

Medium4 of 10

Topics
Greedy, Sorting
Solved
No attempts yet

Problem

Seonjong learned a new sorting algorithm in a lecture. The algorithm sorts an array by repeatedly deleting one element and inserting it again. Seonjong named the method delete and append sort.

One delete and append step has three stages.

  1. Choose one element of the array.
  2. Delete that element from the array.
  3. Insert that element at the very end of the array.

Repeat the step until the array is sorted in ascending order. The same value can appear several times, so the array counts as sorted when no value is larger than the value right after it. For each array, find the smallest number of delete and append steps that sorts it.

Input

The first line has the number of test cases PP (1≤P≤1001 \le P \le 100). The PP data sets follow, and each data set is handled independently.

The first line of a data set has the data set number KK and the array length NN (1≤N≤10001 \le N \le 1000), separated by a space.

The NN positive integers of the array follow on the next lines. Every line except the last one has 10 integers, and the last line has fewer than 10. Each integer is smaller than 10910^9. The same value can be given several times.

Output

For each data set, print the data set number KK and the smallest number of delete and append steps on one line, separated by a space.

Hint

Take the array 1 3 2 of length 3. Delete 3 and insert it at the end, and the array becomes 1 2 3, which is sorted. Only one step was used, so the answer for this array is 1.

Examples3

  1. Example 1

    Input
    3
    1 3
    1 3 2
    2 6
    1 5 2 4 3 6
    3 23
    67890 56312 999999999 12345 23456 38927 45632 100345 98765 23456
    87654 43278 23456 117654 321899 25432 54326 217435 26845 31782
    33456 41234 56213
    
    Expected output
    1 1
    2 3
    3 15
    
  2. Example 2

    Input
    3
    1 3
    2 1 2
    2 4
    3 1 2 1
    3 4
    1 2 1 2
    
    Expected output
    1 1
    2 2
    3 1
    
  3. Example 3

    Input
    3
    11 15
    1 1 999999999 1 2 999999999 2 3 3 1
    1 4 4 4 2
    12 4
    10 20 30 5
    13 6
    1 3 2 4 3 5
    
    Expected output
    11 9
    12 3
    13 3