// HeHe vro (^ _ ^)
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
//
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
//#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
//
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
//
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
//
#define ll long long int
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define sp " "
#define nl "\n"
#define sortv sort(v.begin(), v.end())
#define vin vector<int> v(n);
#define vln vector<ll> v(n);
#define miin map<int, int> m;
#define mln map<ll, ll> m;
#define sin set<int> s;
#define sln set<ll> s;
#define rangev v.begin(), v.end()
#define ranges s.begin(), s.end()
#define autom for (auto &z : m)
#define autov for (auto &z : v)
#define autos for (auto &z : s)
#define fori for (int i = 0; i < n; i++)
#define forj for (int j = 0; j < n; j++)
#define get cin >>
//------------DEBUG--------------------------------------------------------------------------------------------------------
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " " << x << endl;
#else
#define debug(x)
#endif
//-------------------------------------------------------------------------------------------------------------------------
using namespace std;
const string V = "abcdefghijklmnopqrstuvwxyz";
void iota(void)
{ ll c=0; ll s=0;
ll n; cin>>n>>s;
unordered_map<ll,ll>m;
if(s==0) // all are friends to each other
{
for(int i=n;i>0;i--) c+=i;
} else{
for(int i=0;i<s;i++) //here for each individual try to find his nearest enemy
{
ll x,y; cin>>x>>y;
if(y<x)
{
swap(x,y);
}
if(m.find(x)!=m.end())
{
if(m[x]>y) m[x]=y;
}
else
m[x]=y;
}
/*till here we have found out for each individual their nearest enemy, but if we consider a subarray, because of other people
in the sub array the no of subarrays would be affected, suppose 1,2,3,4,5... 4and5 are enemy ,if we individually find nearest enemy for each one then;
1--no enemy(consider 6 to be enemy)
2--no enemy(consider 6 to be enemy)
3--no enemy(consider 6 to be enemy)
4--5
5--we have considered it already...the last element wont affect because there is no one to its right;
now we have to update the nearest enemy for each individual again;
1---5
2---5
3---5
4---5
5--wont affect;
for 1 the subarray is[1,2,3,4,] total subarray(size>1) will be (size-1)=3
simillarly for others we will calculate;
*/
int k=n+1;
for(int i=n-1;i>0;i--)
{
if(m.find(i)!=m.end())
{if(m[i]<k)
k=m[i];}
m[i]=k;
}
c=n;// each individual is also a candidate for answer
autom
{
int f=z.second-z.first;
c+=f-1;
}
}
cout<<c<<nl;
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// freopen("Error.txt", "w", stderr);
//#endif
int t;
t = 1;
cin >> t;
while (t--)
{
iota();
}
}
//