#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int n, k;
int a[MAXN];
bool vis[MAXN];
struct COD {
  int x, y;
} cod[MAXN];
int can[MAXN];
int main() {
  int T;
  cin >> T;
  while (T--) {
    scanf("%d%d", &n, &k);
    int x, y;
    for (int i = 1; i <= 2 * n; i++) vis[i] = false;
    for (int i = 1; i <= k; i++) {
      scanf("%d%d", &x, &y);
      if (x > y) swap(x, y);
      vis[x] = vis[y] = true;
      cod[i].x = x;
      cod[i].y = y;
    }
    int cnt = 0;
    for (int i = 1; i <= 2 * n; i++) {
      if (!vis[i]) {
        can[++cnt] = i;
      }
    }
    int mid = cnt / 2;
    for (int i = 1; i <= n - k; i++) {
      cod[k + i].x = can[i];
      cod[k + i].y = can[i + mid];
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j < i; j++) {
        if (cod[i].x > cod[j].x && cod[i].y > cod[j].y && cod[i].x < cod[j].y)
          ans++;
        if (cod[j].x > cod[i].x && cod[j].y > cod[i].y && cod[j].x < cod[i].y)
          ans++;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
