#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 1;
const int P = 1000000007;
const long long LLINF = 1e18 + 1;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto& i : v) os << i << " ";
  os << "\n";
  return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  os << p.first << " " << p.second;
  return os;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long mod(long long a, long long b) { return ((a % b) + b) % b; }
long long ext_gcd(long long a, long long b, long long& x, long long& y) {
  long long g = a;
  x = 1, y = 0;
  if (b) g = ext_gcd(b, a % b, y, x), y -= a / b * x;
  return g;
}
long long inv(long long a, long long m) {
  long long x, y;
  long long g = ext_gcd(a, m, x, y);
  if (g > 1) return -1;
  return mod(x, m);
}
bool chk(pair<int, int> a, pair<int, int> b) {
  if (a.first > a.second) swap(a.first, a.second);
  if (b.first > b.second) swap(b.first, b.second);
  if (a.first > b.first) swap(a, b);
  return a.second > b.first && b.second > a.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<pair<int, int> > A(k);
    vector<bool> C(2 * n, false);
    for (auto& i : A) {
      cin >> i.first >> i.second;
      C[--i.first] = true;
      C[--i.second] = true;
    }
    vector<int> B;
    for (int i = 0; i < 2 * n; i++)
      if (!C[i]) B.push_back(i);
    for (int i = 0; i < n - k; i++) A.push_back({B[i], B[n - k + i]});
    int cnt = 0;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        if (chk(A[i], A[j])) {
          cnt++;
        }
    cout << cnt << "\n";
  }
  return 0;
}
