#include <bits/stdc++.h>
using namespace std;
struct Query {
  int l, r, x, idx;
};
struct ArrayElement {
  int val, idx;
};
bool cmp1(Query q1, Query q2) { return q1.x < q2.x; }
bool cmp2(ArrayElement x, ArrayElement y) { return x.val < y.val; }
void update(int bit[], int idx, int val, int n) {
  for (; idx <= n; idx += idx & -idx) bit[idx] += val;
}
long query(int bit[], int idx, int n) {
  long sum = 0;
  for (; idx > 0; idx -= idx & -idx) sum += bit[idx];
  return sum;
}
void answerQueries(int n, Query queries[], int q, ArrayElement arr[]) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));
  sort(arr, arr + n, cmp2);
  sort(queries, queries + q, cmp1);
  int curr = 0;
  long ans[q];
  for (int i = 0; i < q; i++) {
    while (arr[curr].val < queries[i].x && curr < n) {
      update(bit, arr[curr].idx + 1, 1, n);
      curr++;
    }
    ans[queries[i].idx] =
        query(bit, queries[i].r + 1, n) - query(bit, queries[i].l, n);
  }
  long sum = 0;
  for (int i = 0; i < q; i++) sum = sum + (ans[i] > 0 ? 1 : 0);
  std::cout << sum << std::endl;
}
int main() {
  int t;
  std::cin >> t;
  while (t-- > 0) {
    int n;
    std::cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) std::cin >> a[i];
    ArrayElement arr[n];
    for (int i = 0; i < n; i++) {
      arr[i].val = a[i];
      arr[i].idx = i;
    }
    int q = n;
    Query queries[q];
    for (int i = 0; i < n; i++) {
      queries[i].l = i + 1;
      queries[i].r = n - 1;
      queries[i].x = a[i];
      queries[i].idx = i;
    }
    answerQueries(n, queries, q, arr);
  }
  return 0;
}
