#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0, 0, 0};
const long long inf = 100000000000;
const int seivesize = 1000005;
const double eps = 0.000000009;
int* seive;
vector<int> primes;
void fill() {
  seive = new int[seivesize];
  primes.clear();
  for (int i = 0; i < seivesize; i++) {
    seive[i] = 1;
  }
  seive[0] = seive[1] = 0;
  for (int i = 2; i < seivesize; i++) {
    if (seive[i] == 1) {
      primes.push_back(i);
      for (int j = i + i; j < seivesize; j = j + i) {
        seive[j] = 0;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    n *= 2;
    vector<long long> st, ed;
    long long used[n];
    memset(used, 0, sizeof(used));
    for (int i = 0; i < k; i++) {
      long long x1, x2;
      cin >> x1 >> x2;
      x1--;
      x2--;
      if (x1 > x2) swap(x1, x2);
      st.push_back(x1);
      ed.push_back(x2);
      used[x1] = 1;
      used[x2] = 1;
    }
    vector<long long> rem;
    for (int i = 0; i < n; i++) {
      if (!used[i]) rem.push_back(i);
    }
    for (int i = 0; i < rem.size() / 2; i++) {
      st.push_back(rem[i]);
      ed.push_back(rem[rem.size() / 2 + i]);
    }
    long long ans = 0;
    map<long long, long long> m;
    for (int i = 0; i < st.size(); i++) {
      m[st[i]] = ed[i];
    }
    for (int i = 0; i < n; i++) {
      if (m.find(i) != m.end()) {
        long long x = i;
        long long y = m[i];
        for (int j = x + 1; j < y; j++) {
          if (m.find(j) != m.end()) {
            if (m[j] > y) ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
