#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<pair<int, int> > A;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      A.push_back(make_pair(x, i));
    }
    sort(A.begin(), A.end());
    int s = 0;
    s = s + A[0].second;
    int m = A[0].second;
    for (int i = 1; i < n; i++) {
      if (A[i].second > m) {
        s = s + A[i].second - m - 1;
        m = A[i].second;
      }
    }
    cout << s << '\n';
  }
}
