#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
using namespace std;
const long long M = 1e9 + 7;
void solution() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(m);
  vector<long long> b(m), c(m, 0);
  for (long long &x : a) cin >> x;
  b = a;
  sort((a).begin(), (a).end());
  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    auto v = upper_bound((a).begin(), (a).end(), b[i]);
    v--;
    long long f = v - a.begin();
    long long first;
    for (long long j = f; j >= 0; j--)
      if (c[j] == 0) {
        c[j] = 1;
        first = j;
        break;
      }
    for (long long k = 0; k < first; k++) ans += c[k];
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  cin >> t;
  while (t--) solution();
  return 0;
}
