#include using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i) #define rep(i,a,b) for(int i(a); i <= (b); ++i) #define dec(i,a,b) for(int i(a); i >= (b); --i) #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) #define LL long long #define ULL unsigned long long #define MP make_pair #define PB push_back #define FI first #define SE second #define INF 1 << 30 const int N = 200000 + 10; const int M = 10000 + 10; const int Q = 1000 + 10; const int A = 30 + 1; struct node{ int id, num, order; } x[N]; int n; int T; int et; LL a[N], b[N], c[N]; int y[N]; int l[N], r[N]; LL f[N << 1]; int mr; LL ans, now; LL p[N << 1]; bool cmp(node a, node b){ return a.num < b.num; } int main(){ #ifndef ONLINE_JUDGE freopen("test.txt", "r", stdin); freopen("test.out", "w", stdout); #endif scanf("%d", &T); while (T--){ et = 0; memset(f, 0, sizeof f); memset(l, 0, sizeof l); memset(r, 0, sizeof r); scanf("%d", &n); memset(x, 0, sizeof x); rep(i, 1, n){ ++et; scanf("%d", &x[et].num); x[et].id = et; ++et; scanf("%d", &x[et].num); x[et].id = et; scanf("%I64d%I64d%I64d", a + i, b + i, c + i); } sort(x + 1, x + et + 1, cmp); rep(i, 1, et) x[i].order = x[i].num == x[i - 1].num ? x[i - 1].order : x[i - 1].order + 1; rep(i, 1, et) y[x[i].id] = x[i].order; et = 0; rep(i, 1, n){ ++et; l[i] = y[et] + 1; l[i] *= 2; ++et; r[i] = y[et] + 1; r[i] *= 2; } mr = 0; rep(i, 1, n) mr = max(mr, r[i]); mr += 2; rep(i, 1, n){ f[1] += c[i]; f[l[i]] -= c[i]; f[l[i]] += a[i]; f[r[i] + 1] -= a[i]; f[r[i] + 1] += b[i]; f[mr + 1] -= b[i]; } ans = 0; now = 0; memset(p, 0, sizeof p); rep(i, 1, mr){ p[i] = p[i - 1] + f[i]; ans = max(ans, p[i]); } printf("%I64d\n", ans); } return 0; }