#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
long long C(int n, int r) {
  if (r > n) return 0;
  if (r > n - r) r = n - r;
  long long ans = 1;
  int i;
  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }
  return ans;
}
vector<long long> sieve(long long n) {
  vector<long long> is_prime(n + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i] && (long long)i * i <= n) {
      for (int j = i * i; j <= n; j += i) is_prime[j] = false;
    }
  }
  return is_prime;
}
static bool cmp(pair<int, int> &p1, pair<int, int> &p2) {
  return p1.second > p2.second;
}
void test() {
  int n, k;
  cin >> n >> k;
  int x, y;
  set<int> a, b;
  for (int i = 1; i <= n; ++i) a.insert(i);
  for (int i = n + 1; i <= 2 * n; ++i) b.insert(i);
  vector<pair<int, int>> v;
  for (long long(i) = 0; (i) < k; ++(i)) {
    cin >> x >> y;
    if (x > y) swap(x, y);
    a.erase(x), a.erase(y);
    b.erase(x), b.erase(y);
    v.push_back({x, y});
  }
  while (a.size() < b.size()) {
    auto it = b.begin();
    a.insert(*it);
    b.erase(it);
  }
  while (a.size() > b.size()) {
    auto it = a.end();
    --it;
    b.insert(*it);
    a.erase(it);
  }
  set<int> ac = a, bc = b;
  vector<pair<int, int>> vc = v;
  for (int i = 0; i < n - k; ++i) {
    auto it1 = a.begin();
    auto it2 = b.begin();
    v.push_back({*it1, *it2});
    a.erase(it1);
    b.erase(it2);
  }
  sort(v.begin(), v.end());
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (v[i].second > v[j].first && v[i].second < v[j].second) ++cnt;
    }
  }
  cout << cnt << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  for (long long T = 0; T < t; T++) {
    test();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\n";
}
