#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    vector<int> A;
    int temp;
    for (int i = 0; i < N; i++) {
      cin >> temp;
      A.push_back(temp);
    }
    stack<int> S;
    vector<int> right;
    for (int i = N - 1; i >= 0; i--) {
      if (S.empty()) {
        right.push_back(-1);
        S.push(i);
      } else {
        while (!S.empty() && A[S.top()] >= A[i]) {
          S.pop();
        }
        if (S.empty()) {
          right.push_back(-1);
        } else {
          right.push_back(S.top());
        }
        S.push(i);
      }
    }
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (right[i] != -1) {
        cnt++;
      }
    }
    cout << cnt << "\n";
  }
}
