description: On a circle lie 2n distinct points, with the following property: however you choose 3 chords that connect 3 disjoint pairs of points, no point strictly inside the circle belongs to all 3 chords. The points are numbered 1,   2,   ...,   2n in clockwise order.
  
  Initially, k chords connect k pairs of points, in such a way that all the 2k endpoints of these chords are distinct.
  
  You want to draw n - k additional chords that connect the remaining 2(n - k) points (each point must be an endpoint of exactly one chord).
  
  In the end, let x be the total number of intersections among all n chords. Compute the maximum value that x can attain if you choose the n - k chords optimally.
  
  Note that the exact position of the 2n points is not relevant, as long as the property stated in the first paragraph holds.
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. Then t test cases follow.
  
  The first line of each test case contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ n) — half the number of points and the number of chords initially drawn.
  
  Then k lines follow. The i-th of them contains two integers x_i and y_i (1 ≤ x_i,   y_i ≤ 2n, x_i ≠ y_i) — the endpoints of the i-th chord. It is guaranteed that the 2k numbers x_1,   y_1,   x_2,   y_2,   ...,   x_k,   y_k are all distinct.
  
  Output
  
  For each test case, output the maximum number of intersections that can be obtained by drawing n - k additional chords.
  
  Example
  
  Input
  
  
  4
  4 2
  8 2
  1 5
  1 1
  2 1
  2 0
  10 6
  14 6
  2 20
  9 10
  13 18
  15 12
  11 7
  
  
  Output
  
  
  4
  0
  1
  14
  
  Note
  
  In the first test case, there are three ways to draw the 2 additional chords, shown below (black chords are the ones initially drawn, while red chords are the new ones):
  
  <image>
  
  We see that the third way gives the maximum number of intersections, namely 4.
  
  In the second test case, there are no more chords to draw. Of course, with only one chord present there are no intersections.
  
  In the third test case, we can make at most one intersection by drawing chords 1-3 and 2-4, as shown below:
  
  <image>
solution: import sys
  import math
  from math import *
  import builtins
  import collections
  import bisect
  import os
  from io import BytesIO, IOBase
  
  
  def print(x, end='\n'):
      sys.stdout.write(str(x) + end)
  
  
  # IO helpers
  def get_int():
      return int(input())
  
  
  def get_list_ints():
      return list(map(int, input().split()))
  
  
  def get_char_list():
      s = input()
      return list(s[:len(s) - 1])
  
  
  def get_tuple_ints():
      return tuple(map(int, input().split()))
  
  
  def print_iterable(p):
      print(" ".join(map(str, p)))
  
  def CountFrequency(arr):
      return collections.Counter(arr) 
   
  def reverse(s):
      l=len(s)
      for i in range(l//2):
          s[i],s[l-i-1]=s[l-i-1],s[i]
      return s
  
  
  def binary_search(arr, x):
      low = 0
      high = len(arr) - 1
      mid = 0
      while low <= high:
          mid = (high + low) // 2
          if arr[mid] < x:
              low = mid + 1
          elif arr[mid] > x:
              high = mid - 1
          else:
              return mid
      return high
  
  #STD lower bound equivalent in python
  def lower_bound(arr,x):
      return bisect.bisect_left(arr, x, lo=0, hi=len(arr))
   
   #STD upper bound equivalent in python
  def upper_bound(arr,x):
       return bisect.bisect_right(arr, x, lo=0, hi=len(arr))
  
  
  def pow(a,n):
      if n==0:
          return 1
      elif n%2==0:
          return ((pow(a,n//2)%const)*(pow(a,n//2)%const))%const
      else:
          return ((pow(a,n//2)%const)*(pow(a,n//2)%const)*(a%const))%const
  
  def count1(list):
      count=0
      for i in list:
          if i==0:
              count+=1
      return count
  def apt(a,b,c,d):
      a1=max(a,b)
      a2=min(a,b)
      if c>a2 and c<a1 and (d<a2 or d>a1):
          return True
      elif  d>a2 and d<a1 and (c<a2 or c>a1):
          return True
      else:
          return False
  
  
  def main():
      n=get_int()
      for i in range(n):
          [p,k]=get_list_ints()
          val=p-k
          ans=(val*(val-1))//2
          list=[0 for i in range(2*p)]
          pointl=[]
          for i in range(k):
              [x,y]=get_list_ints()
              list[x-1]=1
              list[y-1]=1
              pointl.append((x,y))
          for (x,y) in pointl:
              x1=min(x,y)
              y1=max(x,y)
              ans+=min(count1(list[x1:y1-1]),count1(list[:x1-1]+list[y1:]))
          l=len(pointl)
          initial=0
          for i in range(l-1):
              for j in range(i+1,l):
                  if apt(pointl[i][0],pointl[i][1],pointl[j][0],pointl[j][1])==True:
                      initial+=1
          print(ans+initial)
      pass
  
  
  ##Fast Input Output
  
  BUFSIZE = 8192
  
  
  class FastIO(IOBase):
      newlines = 0
  
      def __init__(self, file):
          self._fd = file.fileno()
          self.buffer = BytesIO()
          self.writable = "x" in file.mode or "r" not in file.mode
          self.write = self.buffer.write if self.writable else None
  
      def read(self):
          while True:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
              if not b:
                  break
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines = 0
          return self.buffer.read()
  
      def readline(self):
          while self.newlines == 0:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
              self.newlines = b.count(b"\n") + (not b)
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines -= 1
          return self.buffer.readline()
  
      def flush(self):
          if self.writable:
              os.write(self._fd, self.buffer.getvalue())
              self.buffer.truncate(0), self.buffer.seek(0)
  
  
  class IOWrapper(IOBase):
      def __init__(self, file):
          self.buffer = FastIO(file)
          self.flush = self.buffer.flush
          self.writable = self.buffer.writable
          self.write = lambda s: self.buffer.write(s.encode("ascii"))
          self.read = lambda: self.buffer.read().decode("ascii")
          self.readline = lambda: self.buffer.readline().decode("ascii")
  
  
  sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  input = lambda: sys.stdin.readline().rstrip("\r\n")
  
  # endregion
  
  if __name__ == "__main__":
      main()