var t,ll,n,i,j,tot,now:longint; l,r,a,b,c:array[0..50007] of longint; ti,w:array[0..500007] of longint; s:array[0..500007] of int64; ans,noww:int64; procedure qsort(lx,rx:longint); var i,j,m,t:longint; begin i:=lx;j:=rx;m:=ti[random(rx-lx+1)+lx]; repeat while (ti[i]m) do dec(j); if (i<=j) then begin t:=ti[i];ti[i]:=ti[j];ti[j]:=t; inc(i);dec(j); end; until (i>j); if (ilx) then qsort(lx,j); end; function find1(x:longint):longint; var left,right,mid:longint; begin left:=1;right:=tot; while (right-left>1) do begin mid:=(left+right) div 2; if (w[mid]-x>=0) then right:=mid else left:=mid; end; if (w[left]=x) then find1:=left else find1:=right; end; begin randomize; readln(t); for ll:=1 to t do begin readln(n); fillchar(l,sizeof(l),0); fillchar(r,sizeof(r),0); fillchar(a,sizeof(a),0); fillchar(b,sizeof(b),0); fillchar(c,sizeof(c),0); for i:=1 to n do begin readln(l[i],r[i],a[i],b[i],c[i]); l[i]:=l[i]+l[i];r[i]:=r[i]+r[i]; end; fillchar(ti,sizeof(ti),0); for i:=1 to n do begin ti[i]:=l[i]; ti[i+n]:=r[i]; end; qsort(1,2*n); tot:=2;w[1]:=ti[1];w[2]:=ti[1]+1; for i:=2 to 2*n do if (ti[i]<>ti[i-1]) then begin inc(tot); w[tot]:=ti[i]; inc(tot); w[tot]:=ti[i]+1; end; fillchar(s,sizeof(s),0); for i:=1 to n do begin now:=find1(l[i]); s[now]:=s[now]+a[i]-c[i]; now:=find1(r[i])+1; s[now]:=s[now]+b[i]-a[i]; end; ans:=0; for i:=1 to n do ans:=ans+c[i]; noww:=ans; for i:=1 to tot+1 do begin noww:=noww+s[i]; if (noww>ans) then ans:=noww; end; writeln(ans); end; end.