#include <bits/stdc++.h>
long long read() {
  long long a = 0;
  int op = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') op = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    a = (a << 3) + (a << 1) + (48 ^ ch);
    ch = getchar();
  }
  return a * op;
}
const int N = 1000;
using pii = std::pair<int, int>;
std::vector<pii> v;
std::vector<int> pos;
bool bl[N];
int intersect(pii a, pii b) {
  if (a.first > b.first) std::swap(a, b);
  return b.first < a.second && a.second < b.second;
}
int calc() {
  int ret = 0;
  for (int i = 0; i < (int)v.size(); i++)
    for (int j = i + 1; j < (int)v.size(); j++) ret += intersect(v[i], v[j]);
  return ret;
}
void solve() {
  v.clear();
  pos.clear();
  int n = read();
  int k = read();
  for (int i = 1; i <= 2 * n; i++) bl[i] = true;
  for (int i = 0; i < k; i++) {
    int p1 = read();
    int p2 = read();
    if (p1 > p2) std::swap(p1, p2);
    bl[p1] = false;
    bl[p2] = false;
    v.emplace_back(p1, p2);
  }
  for (int i = 1; i <= 2 * n; i++)
    if (bl[i]) pos.push_back(i);
  int ans = 0;
  for (int i = 0; i < n - k; i++) {
    int p1 = pos[i];
    int p2 = pos[i + n - k];
    v.emplace_back(p1, p2);
  }
  ans = calc();
  printf("%d\n", ans);
}
int main() {
  int t = read();
  while (t--) solve();
  return 0;
}
