#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define F1(x,y,z) for(int x=(y);x<(z);x++) #define F2(x,y,z) for(int x=(y);x<=(z);x++) #define F3(x,y,z) for(int x=(y);x>(z);x--) #define F4(x,y,z) for(int x=(y);x>=(z);x--) #define mp make_pair #define pb push_back #define LL long long #define co complex #define fi first #define se second #define MAX 100005 #define AMAX 1025*1005 #define MOD 1000000007 #define f(c,d) ((1<<(c))*(d)) using namespace std; int t,n; pair x[MAX]; LL ta,tb,tc,td,ans,l,r; int main(){ scanf("%d",&t); while(t--){ ta=0; scanf("%d",&n); F1(a,0,n){ scanf("%I64d%I64d%I64d%I64d%I64d",&l,&r,&tb,&tc,&td); ta+=td; x[a*2]=mp(l*2,tb-td); x[a*2+1]=mp(r*2+1,tc-tb); } sort(x,x+n*2); x[n*2].fi=0; ans=ta; F1(a,0,n*2){ ta+=x[a].se; if(x[a].fi!=x[a+1].fi)ans=max(ans,ta); } printf("%I64d\n",ans); } #ifdef LOCAL_PROJECT system("pause"); #endif return 0; }