#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int M = 1e6;
const long long INF = (long long)(1e18);
const int inf = 1e9;
const long long MOD = 1000000007LL;
void solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  set<int> st;
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    auto it = st.lower_bound(arr[i]);
    if (it != st.begin()) ans++;
    st.insert(arr[i]);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
