func_code_string: #include <bits/stdc++.h>   inline long long read() {     long long x = 0;     char ch = getchar(), w = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * w;   }   void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   inline void writeln(long long x) {     write(x);     puts(  );   }   using namespace std;   const int N = 5e5 + 656666;   long long n, m, c, c0;   struct TT {     long long t, a, b;   } a[N];   bool cmp(TT x, TT y) { return x.t < y.t; }   map<long long, long long> q;   long long cal() {     q.clear();     long long ans = 0;     q[0] = c0;     long long now = c0, cap = c;     for (int i = 1; i <= n; ++i) {       long long rest = a[i].t - a[i - 1].t;       long long val = 0;       now -= rest;       while (!q.empty() && rest) {         val = min(rest, q.begin()->second);         rest -= val;         q.begin()->second -= val;         ans += val * q.begin()->first;         if (!q.begin()->second) q.erase(q.begin());       }       if (rest) return -1;       long long ned = min(cap - now, a[i].a);       now += ned;       while (!q.empty() && a[i].b < q.rbegin()->first && ned < a[i].a) {         val = min(q.rbegin()->second, a[i].a - ned);         q.rbegin()->second -= val;         ned += val;         if (!q.rbegin()->second) q.erase(--q.end());       }       q[a[i].b] += ned;     }     return ans;   }   int main() {     int T = read();     while (T--) {       n = read(), m = read(), c = read(), c0 = read();       for (int i = 1; i <= n; ++i) {         a[i].t = read();         a[i].a = read();         a[i].b = read();       }       a[++n] = (TT){m, 0, 0};       sort(a + 1, a + 1 + n, cmp);       writeln(cal());     }     return 0;   }   