#include <bits/stdc++.h>
using namespace std;
const long long M = 2e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long T;
  cin >> T;
  for (long long tc = 1; tc <= T; tc++) {
    long long n;
    cin >> n;
    map<long long, long long> pos;
    vector<long long> v;
    for (long long i = 0, x; i < n; i++) {
      cin >> x;
      v.push_back(x);
      pos[x] = i;
    }
    sort(v.begin(), v.end());
    long long cpos = -1;
    long long cnt = 0, ans = 0;
    for (long long i = 0; i < n; i++) {
      if (pos[v[i]] > cpos) {
        cnt++;
      } else {
        ans = max(ans, cnt);
        cnt = 1;
      }
      cpos = pos[v[i]];
    }
    ans = max(ans, cnt);
    cout << n - ans << endl;
  }
  return 0;
}
