#include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef unsigned long long ULL; const int MaxN = 1e5; int T, n; vectorin[MaxN + 5]; vectorout[MaxN + 5]; struct Point{ int l, r; LL d, m, h; }a[MaxN + 5]; struct Node{ double loc; int pos; }p[MaxN + 5]; bool cmp(Node x, Node y) {return x.loc < y.loc;} int main() { scanf("%d", &T); while(T--) { LL ans = 0, res = 0; scanf("%d", &n); for(int i = 1; i <= n; i++) { double l, r; scanf("%lf%lf%I64d%I64d%I64d", &l, &r, &a[i].m, &a[i].h, &a[i].d); p[i * 2 - 1].loc = l; p[i * 2 - 1].pos = i * 2 - 1; p[i * 2].loc = r + 0.1; p[i * 2].pos = i * 2; } sort(p + 1, p + 2 * n + 1, cmp); int pos = 1; if(p[1].pos & 1) a[(p[1].pos + 1) / 2].l = pos; else a[(p[1].pos / 2)].r = pos; for(int i = 2; i <= 2 * n; i++) { if(p[i].loc == p[i - 1].loc) { if(p[i].pos & 1) a[(p[i].pos + 1) / 2].l = pos; else a[(p[i].pos / 2)].r = pos; } else { pos++; if(p[i].pos & 1) a[(p[i].pos + 1) / 2].l = pos; else a[(p[i].pos / 2)].r = pos; } } for(int i = 1; i <= n; i++) { in[a[i].l].push_back(i); out[a[i].r].push_back(i); res += a[i].d; } for(int i = 0; i <= pos; i++) { while(!in[i].empty()) { int x = in[i].back(); res -= a[x].d; res += a[x].m; in[i].pop_back(); } while(!out[i].empty()) { int x = out[i].back(); res -= a[x].m; res += a[x].h; out[i].pop_back(); } ans = max(ans, res); } printf("%I64d\n", ans); } }