#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, x;
    cin >> n;
    vector<pair<int, int> > v;
    for (int f = 0; f < n; f++) {
      cin >> x;
      v.push_back({x, f});
    }
    sort(v.begin(), v.end());
    int i = 0, ans = 0;
    multiset<int> s;
    for (int f = 0; f < n; f++) {
      while (s.size() && -(*s.begin()) > v[f].second) {
        s.erase(-v[i].second);
        i++;
      }
      s.insert(-v[f].second);
      ans = max(ans, (int)s.size());
    }
    cout << n - ans << endl;
  }
}
