import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

    // We seek a longest increasing subsequence (LIS) of the input (the input being
    // a permutation of the integers 1..n). Intuitively, the items in the LIS
    // remain fixed, and the remaining n-|LIS| items are moved across segments of the 
    // LIS at a cost of one step per move. Thus reordering can be performed
    // in n-|LIS| steps. To find some LIS, we use dynamic programming to find a
    // longest commons subsequence (LCS) between the input and the ordered
    // sequence 1,2,...,n. (e.g., see Cormen, Leiserson, Rivest).

public class ReorderN {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        int numCases = Integer.parseInt(in.readLine());
        for (int i = 0; i < numCases; i++){
            List l = new LinkedList();
            StringTokenizer toks = new StringTokenizer(in.readLine());
            while (toks.hasMoreTokens()) l.add(new Integer(toks.nextToken()));
            System.out.println(l.size() - LIS(l));
        }
    }

    // LIS is just LCS against a sorted version of itself
    static int LIS(List l){
        List copy = new ArrayList(l);
        Collections.sort(copy);

        return LCS(l, copy);
    }

    static int LCS(List a, List b){
        int[][] m = new int[a.size()+1][b.size()+1];
        
        // note that our lists are zero-based, but are thought of as
        // one-based w.r.t the matrix because it has a buffer of 0's
        // around the upper and left borders
        for (int i = 1; i <= a.size(); i++)
            for (int j = 1; j <= b.size(); j++)
                if (a.get(i-1).equals(b.get(j-1)))
                    m[i][j] = 1 + m[i-1][j-1];
                else 
                    m[i][j] = Math.max(m[i-1][j], m[i][j-1]);

        return m[a.size()][b.size()];
    }

}
