#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = LLONG_MAX;
const long long N = 1e5 + 5;
signed main() {
  ios_base ::sync_with_stdio(false);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    stack<long long> stk;
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      long long x;
      cin >> x;
      if (stk.empty())
        stk.push(x);
      else {
        while (!stk.empty() && stk.top() > x) {
          stk.pop();
          ans++;
        }
        stk.push(x);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
