#include #include #include #include #include #define ll long long #define mst(a,x) memset(a,x,sizeof(a)) #define For(i, t) for(int i = 0; i < t; i++) using namespace std; const int INF = 2 * 1e9 + 5; const int M = 5 * 1e4 + 5; const int N = 4 * M; int y[N], tot; struct Plant{ int l, r, a, b, c; void Read() { scanf("%d%d%d%d%d", &l, &r, &a, &b, &c); l *= 2; r *= 2; y[tot++] = l; y[tot++] = r; y[tot++] = l - 1; y[tot++] = r + 1; } }a[M]; ll b[N]; int main() { int T; scanf("%d", &T); while(T--) { int n; scanf("%d", &n); tot = 0; For(i, n) a[i].Read(); y[tot++] = 0; y[tot++] = INF; sort(y, y + tot); tot = unique(y, y + tot) - y; For(i, tot) b[i] = 0; For(i, n) { int L = lower_bound(y, y + tot, a[i].l) - y; int R = lower_bound(y, y + tot, a[i].r) - y; b[L] += a[i].a; b[R + 1] -= a[i].a; b[0] += a[i].c; b[L] -= a[i].c; b[R + 1] += a[i].b; } ll sum = 0, ans = 0; For(i, tot) { sum += b[i]; ans = max(ans, sum); } printf("%I64d\n", ans); } return 0; }