#include <bits/stdc++.h>
using namespace std;
template <class T>
void minn(T &A, T B) {
  A = min(A, B);
}
template <class T>
void maxx(T &A, T B) {
  A = max(A, B);
}
template <class T>
void summ(T &A, T B) {
  A = A + B;
}
template <class T>
void hieu(T &A, T B) {
  A = A - B;
}
int n, a, t;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    vector<pair<int, int>> p;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a;
      p.push_back({a, i});
    }
    int ans = 1, res = 0;
    sort(p.begin(), p.end());
    for (int i = 0; i < p.size() - 1; ++i) {
      if (p[i].second < p[i + 1].second) {
        ans++;
      } else
        ans = 1;
      maxx(res, ans);
    }
    maxx(res, ans);
    cout << n - res << endl;
  }
}
