#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    n = n * m;
    vector<long long> T;
    long long incon = 0;
    for (long long i = 0; i < n; i++) {
      long long temp;
      cin >> temp;
      T.push_back(temp);
      for (long long j = 0; j < T.size() - 1; j++) {
        if (temp > T[j]) {
          incon++;
        }
      }
    }
    cout << incon << "\n";
  }
}
