#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int arr = 200005;
int pow(long long int x, long long int y, long long int p) {
  if (y == 0) return 1;
  if (y == 1) return x;
  x = x % p;
  long long int ans = pow(x, y / 2, p) % p;
  ans = ans * ans % p;
  if (y % 2 == 0)
    return ans % p;
  else
    return ans * x % p;
}
void solve() {
  int n, k;
  cin >> n;
  int ans = 1;
  vector<pair<int, int> > a(n);
  for (int i = 0; i < n; i++) {
    cin >> k;
    a[i] = {k, i + 1};
  }
  sort(a.begin(), a.end());
  ;
  int temp = 1;
  for (int i = 1; i < n; i++) {
    if (a[i].second > a[i - 1].second) {
      temp++;
    } else {
      ans = max(ans, temp);
      temp = 1;
    }
  }
  ans = max(ans, temp);
  cout << n - ans << "\n";
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int t = 1;
  cin >> t;
  int teh = t;
  while (t--) {
    solve();
  }
  return 0;
}
