#include #include #include #include #include using namespace std; typedef __int64 ll; ll sum[50005*4]; ll add[50005*4]; int n,maxm; const int ed=1e9+7; vectorv; struct node{ int l,r,a,b,c; }p[50005]; int getid(int x){ return (lower_bound(v.begin(),v.end(),x)-v.begin()+1)*2; } void work(int l,int r,int x){ if(l>r)return ; add[l]+=x; add[r+1]-=x; } int main() { int t; scanf("%d",&t); while(t--){ v.clear(); memset(sum,0,sizeof(sum)); memset(add,0,sizeof(add)); int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d%d%d%d",&p[i].l,&p[i].r,&p[i].a,&p[i].b,&p[i].c); v.push_back(p[i].l); v.push_back(p[i].r); } v.push_back(0); v.push_back(ed); sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); maxm=v.size()*2; for(int i=1;i<=n;i++){ int l,r; l=getid(p[i].l),r=getid(p[i].r); work(l,r,p[i].a); work(r+1,maxm,p[i].b); work(1,l-1,p[i].c); } ll step=0; ll ans=0; for(int i=1;i<=maxm;i++){ sum[i]=sum[i-1]+add[i]; if(sum[i]>ans)ans=sum[i]; } printf("%I64d\n",ans); } return 0; }