#include <bits/stdc++.h>
using namespace std;
int b[150][2];
int uo[305];
int n;
int k;
int T(int x) { return (x * (x + 1)) / 2; }
int f(int x, int y) {
  int A = b[x][0];
  int B = b[x][1];
  if (A > B) {
    int t = A;
    A = B;
    B = t;
  }
  int C = b[y][0];
  int D = b[y][1];
  int cnt = 0;
  if (A <= C && C <= B) cnt++;
  if (A <= D && D <= B) cnt++;
  return cnt == 1;
}
int g(int x, int y) {
  int A;
  if (x > y) {
    int t = x;
    x = y;
    y = t;
  }
  A = y - x - 1 - (uo[y - 1] - uo[x]);
  return max(min(2 * n - 2 * k - A, A), 0);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> k;
    for (int i = 0; i <= 2 * n; i++) uo[i] = 0;
    for (int i = 0; i < k; i++) {
      cin >> b[i][0] >> b[i][1];
      uo[b[i][0]]++;
      uo[b[i][1]]++;
    }
    for (int i = 1; i <= 2 * n; i++) {
      uo[i] += uo[i - 1];
    }
    int ans = T(n - k - 1);
    for (int i = 0; i < k; i++) {
      for (int j = i + 1; j < k; j++) {
        ans = ans + f(i, j);
      }
    }
    for (int i = 0; i < k; i++) {
      ans = ans + g(b[i][0], b[i][1]);
    }
    cout << ans << "\n";
  }
}
