#include <bits/stdc++.h>
using namespace std;
const long long mxN = 1e3;
const int mod = 1e9 + 7;
const int INF = 1e9;
long long n, k, m, x, y;
void solve() {
  cin >> n;
  vector<array<int, 2>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
    a[i][1] = i;
  }
  sort(a.begin(), a.end());
  int cur = 1, best = 1;
  for (int i = 1; i < n; i++) {
    if (a[i][1] > a[i - 1][1]) {
      cur++;
    } else {
      cur = 1;
    }
    best = max(best, cur);
  }
  cout << n - best << endl;
}
signed main() {
  cout << setprecision(12) << fixed;
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
