#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void Code_By_Mohamed_Khaled() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll n,m;
ll sol(ll p) {
    ll l=1,r=n;
    while (l<r) {
        ll mid=l+(r-l)/2;
        if (mid*(mid+1)/2>=p)r=mid;
        else l=mid+1;
    }
    return l;
}
int main() {
    Code_By_Mohamed_Khaled();
    // freopen("box.in", "r", stdin);
    ll t;cin>>t;
    while(t--) {
        cin>>n>>m;
        ll l=n*(n+1)/2;
        ll ans=n*(n-1)/2;
        map<ll,ll>mp;
        auto get= [&](ll p)->ll {
            auto it=mp.find(p);
            if (it !=mp.end()) return it->second;
            return sol(p);
        };
        auto set= [&](ll p, ll v){
            mp[p]=v;
        };
        for (ll i=1;i<=m;i++) {
            ll x,y;cin>>x>>y;
            if (x==y)continue;
            array<ll,4>v={x-1, x, y-1, y};
            ll before = 0, after = 0;
            for (ll p:v) {
                if (p>=1 and p+1<=l and get(p)==get(p+1)) before++;
            }
            ll vx=get(x),vy=get(y);
            set(x,vy);
            set(y,vx);
            for (ll p:v) {
                if (p>=1 and p+1<=l and get(p)==get(p+1))after++;
            }
            ans+=(after-before);
        }
        cout<<ans<<"\n";
    }
    return 0;
}