#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  auto solve = [&]() {
    int N, K;
    cin >> N >> K;
    vector<pair<int, int> > A(N);
    for (int i = 0; i < K; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      if (a > b) {
        swap(a, b);
      }
      A[i] = {a, b};
    }
    auto inter = [&](pair<int, int> a, pair<int, int> b) {
      if (a.first > b.first) {
        swap(a, b);
      }
      vector<int> v1{a.first, b.first, b.second, a.second},
          v2{a.first, a.second, b.first, b.second};
      return !(is_sorted(v1.begin(), v1.end()) ||
               is_sorted(v2.begin(), v2.end()));
    };
    vector<int> have(2 * N);
    for (int i = 0; i < K; i++) {
      auto [a, b] = A[i];
      have[a] = have[b] = 1;
    }
    vector<int> pos;
    for (int i = 0; i < 2 * N; i++) {
      if (!have[i]) {
        pos.push_back(i);
      }
    }
    assert((int)pos.size() == 2 * (N - K));
    int ans = 0;
    for (int j = 0; j < N - K; j++) {
      A[j + K] = {pos[j], pos[j + N - K]};
    }
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        ans += inter(A[i], A[j]);
      }
    }
    cout << ans << '\n';
  };
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
