#include <bits/stdc++.h>
#pragma GCC optimize("O3")
int const N = 1e5 * 4;
using namespace std;
long long n, c, a, b, o, q, summ, maxx, minn, arr[N];
vector<long long> v;
map<long long, long long> vv;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  minn = 1e18;
  while (n--) {
    cin >> a;
    for (int i = 0; i < a; i++) {
      cin >> b;
      v.push_back(b);
    }
    for (int i = a - 1; i >= 0; i--) {
      if (minn == v[i]) continue;
      minn = min(minn, v[i]);
      vv[minn]++;
    }
    for (auto q : vv) summ += (q.second) - 1;
    cout << summ << endl;
    vv.clear();
    v.clear();
    minn = 1e18;
    summ = 0;
  }
  return 0;
}
