#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, k;
    cin >> n >> k;
    vector<array<int, 2>> chords;
    vector<int> used(2 * n, 0);
    for (int i = 0; i < k; ++i) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      if (x > y) swap(x, y);
      chords.push_back({x, y});
      used[x] = 1;
      used[y] = 1;
    }
    vector<int> unused;
    for (int i = 0; i < 2 * n; ++i) {
      if (!used[i]) unused.push_back(i);
    }
    for (int i = 0; i < n - k; ++i) {
      chords.push_back({unused[i], unused[i + n - k]});
    }
    assert((int)chords.size() == n);
    sort(begin(chords), end(chords));
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        ans += (chords[j][0] < chords[i][1] && chords[j][1] > chords[i][1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
