#include <bits/stdc++.h>
using namespace std;
void soivez() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
int main() {
  int t;
  cin >> t;
  vector<int> ans(t);
  for (int i = 0; i < t; i++) {
    int count = 0;
    int n;
    cin >> n;
    stack<int> s;
    for (int j = 0; j < n; j++) {
      int k;
      cin >> k;
      while (!s.empty() && k < s.top()) {
        s.pop();
        count++;
      }
      s.push(k);
    }
    ans[i] = count;
  }
  for (int i = 0; i < t; i++) {
    cout << ans[i] << "\n";
  }
  cout << endl;
}
