#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, a;
    cin >> n;
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++) {
      cin >> a;
      v.push_back({a, i});
    }
    sort(v.begin(), v.end());
    int cnt = 0, pos = -1, sol = 1;
    for (int i = 0; i < n; i++) {
      if (v[i].second < pos) {
        cnt = 1;
      } else {
        cnt++;
      }
      pos = v[i].second;
      sol = max(sol, cnt);
    }
    cout << n - sol << endl;
  }
}
