#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    long long b[n];
    b[n - 1] = -1;
    stack<long long> s;
    s.push(n - 1);
    for (long long i = n - 2; i >= 0; i--) {
      if (s.empty()) {
        s.push(i);
      } else {
        while (!s.empty() && a[s.top()] >= a[i]) {
          s.pop();
        }
        if (s.empty()) {
          b[i] = -1;
        } else {
          b[i] = a[s.top()];
        }
        s.push(i);
      }
    }
    long long count = 0;
    for (long long i = 0; i < n; i++) {
      if (b[i] != -1) {
        count++;
      }
    }
    cout << count << endl;
  }
}
