#include <bits/stdc++.h>
const long long mod = 998244353;
using namespace std;
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, ans = 0;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    deque<long long> q;
    for (long long i = 0; i < n; i++) {
      while (!q.empty() && q.back() > a[i]) {
        q.pop_back();
        ans++;
      }
      q.push_back(a[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}
