#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e5+10;
int n,tot,ans,m,topz,limz,topy,limy,md;
int lsh[N<<1];
vector<int> bj[2][N<<1];
struct node{
int l1,r1,l2,r2;
}a[N];
bool cmp(node x,node y){
if(x.l2!=y.l2) return x.l2<y.l2;
return x.r2<y.r2;
}
void clear(){
tot=ans=topz=limz=topy=limy=md=0;
for(int i=0;i<=m;i++) bj[0][i].clear(),bj[1][i].clear();
return;
}
bool check(int l1,int r1,int l2,int r2){
return !(r1<l2 || r2<l1);
}
signed main(){
freopen("lucky.in","r",stdin);
freopen("lucky.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int ID,T;
cin>>ID>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;++i) cin>>a[i].l1>>a[i].r1>>a[i].l2>>a[i].r2,lsh[++tot]=a[i].l2,lsh[++tot]=a[i].r2,limy+=a[i].l1,topy+=a[i].r1;
sort(lsh+1,lsh+1+tot);
m=unique(lsh+1,lsh+1+tot)-lsh-1;
for(int i=1,x,y;i<=n;++i){
x=lower_bound(lsh+1,lsh+1+m,a[i].l2)-lsh,y=lower_bound(lsh+1,lsh+1+m,a[i].r2)-lsh;
bj[0][x].push_back(i);
bj[1][y].push_back(i);
}
for(int i=1;i<=m;++i){
for(int x : bj[1][i-1]) md-=a[x].r1,topz+=a[x].r1,limz+=a[x].l1;
if(md>0 && (check(limy,topy,limz,topz) || (topy<limz && (topy+md)*2>((topy+limz+md))) || (topz<limy && topz+md>=((topz+limy+md+1)/2)))) ans+=lsh[i]-lsh[i-1]-1;
for(int x : bj[0][i]) md+=a[x].r1,topy-=a[x].r1,limy-=a[x].l1;
if(md>0 && (check(limy,topy,limz,topz) || (topy<limz && (topy+md)*2>((topy+limz+md))) || (topz<limy && topz+md>=((topz+limy+md+1)/2)))) ans++;
}
cout<<ans<<'\n';
clear();
}
return 0;
}