#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long testcases;
  cin >> testcases;
  for (int tcc = 0; tcc < testcases; tcc++) {
    long long sum = 0, flag = 0, ans = 0;
    long long i, j, k, x, y, z;
    long long n, m, l, r, p, t, q;
    cin >> m >> n;
    map<long long, long long> mp1, mp2;
    map<long long, vector<long long> > mp;
    y = 0;
    long long a[n], b[n];
    for (i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    sort(b, b + n);
    x = 0;
    for (i = 0; i < n; i++) {
      x = b[i];
      mp[b[i]].push_back(i + 1);
    }
    set<long long> st;
    for (i = 0; i < n; i++) {
      x = mp[a[i]].back();
      mp[a[i]].pop_back();
      y = 0;
      for (auto &q : st) {
        if (q < x)
          y++;
        else {
          break;
        }
      }
      ans += y;
      st.insert(x);
    }
    cout << ans;
    cout << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  solve();
  return 0;
}
