#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int start[3000];
    int target[3000];
    int canon[3000];
    for (int i = 0; i < n; i++) {
      cin >> start[i];
      target[i] = start[i];
    }
    sort(target, target + n);
    int length = n;
    int i, j, k;
    for (i = 0; i < length; i++) {
      for (k = 0; start[i] != target[k]; k++)
        ;
      canon[i] = k;
    }
    int longuest_start;
    int longuest_length = 0;
    for (i = 0; i < length; i++) {
      k = 1;
      for (j = i + 1; j < length; j++) {
        if (canon[i] + k == canon[j]) k++;
      }
      if (k >= longuest_length) {
        longuest_start = canon[i];
        longuest_length = k;
      }
    }
    int ans = 0;
    while (longuest_length < length) {
      for (i = 0; i < length; i++) {
        if (canon[i] + 1 == longuest_start) {
          k = canon[i];
          for (j = i; j > 0; j--) canon[j] = canon[j - 1];
          canon[0] = k;
          longuest_start--;
          longuest_length++;
          ans++;
        } else if (canon[i] == longuest_start + longuest_length) {
          k = canon[i];
          for (j = i; j < length - 1; j++) canon[j] = canon[j + 1];
          canon[length - 1] = k;
          longuest_length++;
          i--;
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
