#include <bits/stdc++.h>
int ans = 0;
int n, k, p[205][205], rd[205], dr[205];
bool vis[205];
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
void clear() {
  for (int i = 0; i <= n * 2 + 2; i++)
    for (int j = 0; j <= n * 2 + 2; j++)
      p[i][j] = rd[i] = dr[i] = 0, vis[i] = false;
}
int move(int x, int y) {
  x += y;
  if (x < 1) x = 2 * n;
  if (x > 2 * n) x = 1;
  return x;
}
void add(int u, int v) {
  int i = u, j = u;
  while (i != v) {
    i = move(i, 1);
    if (i == v) break;
    j = u;
    while (j != v) {
      j = move(j, -1);
      if (j == v) break;
      p[j][i]++;
    }
  }
}
inline int get(int u, int v) { return min(p[u][v], p[v][u]); }
void run() {
  int u, v;
  clear();
  ans = 0;
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &u, &v);
    ans += get(u, v);
    vis[u] = true;
    vis[v] = true;
    add(u, v);
    add(v, u);
  }
  int s = n - k, cnt = 0;
  for (int i = 1; i <= n * 2; i++)
    if (!vis[i]) rd[i] = ++cnt, dr[cnt] = i;
  for (int i = 1; i <= n * 2; i++)
    if (!vis[i]) {
      int l = rd[i];
      u = i;
      v = dr[(l + s - 1) % cnt + 1];
      ans += get(u, v);
      vis[u] = true;
      vis[v] = true;
      add(u, v);
      add(v, u);
    }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    run();
    printf("%d\n", ans);
  }
  return 0;
}
