#include <bits/stdc++.h>
using namespace std;
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e6 + 2;
const int M = 1e5;
const int mod = 0;
const int inf = 2e9 + 3;
const long long INF = 1e18;
const long double pi2 = 2.0 * 3.141592;
const long double pi = 3.141592;
const long double eps = 1e-12;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
void files(string s = "main") {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
int add(int a, int b) {
  if (a + b < 0) return a + b + mod;
  if (a + b >= mod) return a + b - mod;
  return a + b;
}
int mul(int a, int b) { return a * 1LL * b % mod; }
int binpow(int a, int n) {
  int ret = 1;
  while (n) {
    if (n & 1) ret = mul(ret, a);
    a = mul(a, a);
    n >>= 1;
  }
  return ret;
}
int T, n, k, x[200], y[200];
int is[400];
vector<int> v;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> T;
  while (T--) {
    cin >> n >> k;
    n <<= 1;
    v.clear();
    for (int i = 1; i <= n; ++i) is[i] = 0;
    for (int i = 1; i <= k; ++i) {
      cin >> x[i] >> y[i];
      is[x[i]] = is[y[i]] = 1;
    }
    for (int i = 1; i <= n; ++i)
      if (!is[i]) v.push_back(i);
    for (int i = 0; i < (int)v.size() / 2; ++i) {
      x[k + 1] = v[i], y[++k] = v[i + (int)v.size() / 2];
    }
    for (int i = 1; i <= k; ++i)
      if (x[i] > y[i]) swap(x[i], y[i]);
    int ans = 0;
    for (int i = 1; i <= k; ++i)
      for (int j = 1; j <= k; ++j)
        if (x[i] < x[j] && x[j] < y[i] && y[i] < y[j] && y[j] < x[j] + n) ans++;
    cout << ans << '\n';
  }
}
