#include <bits/stdc++.h>
using namespace std;
stack<int> Q;
int ans = 0;
int temp;
bool flag;
int main() {
  int t;
  int n;
  cin >> t;
  while (t--) {
    ans = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &temp);
      flag = false;
      while (Q.size() && Q.top() > temp) {
        flag = true;
        Q.pop();
        ans++;
      }
      Q.push(temp);
    }
    printf("%d\n", ans);
    while (Q.size()) Q.pop();
  }
}
