#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> m >> n;
  vector<pair<int, int>> a(n);
  int c = 0;
  for (auto&& x : a) {
    cin >> x.first;
    x.second = -(++c);
  }
  sort(a.begin(), a.end());
  set<int> st;
  int ans = 0;
  for (auto [f, s] : a) {
    s = -s;
    auto it = st.lower_bound(s);
    if (it != st.begin()) ans += (int)distance(st.begin(), it);
    st.insert(s);
  }
  cout << ans;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
    cout << '\n';
  }
  return 0;
}
