#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
bool intersect(int l1, int r1, int l2, int r2) {
  if (l1 < l2 && l2 < r1 && !(l1 < r2 && r2 < r1)) return 1;
  if (!(l1 < l2 && l2 < r1) && (l1 < r2 && r2 < r1)) return 1;
  return 0;
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> g(2 * n), used(2 * n);
  for (int i = 0; i < k; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u] = v;
    g[v] = u;
    used[u] = used[v] = 1;
  }
  vector<int> a;
  for (int i = 0; i < 2 * n; ++i)
    if (!used[i]) a.push_back(i);
  int m = a.size();
  ll ans = 0;
  for (int i = 0; i < m / 2; ++i)
    g[a[i]] = a[m / 2 + i], g[a[m / 2 + i]] = a[i];
  for (int i = 0; i < 2 * n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (g[i] < i || g[j] < j) continue;
      ans += intersect(i, g[i], j, g[j]);
    }
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
