#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 3;
inline char gc() {
  static char buf[1 << 21], *S = buf, *T = buf;
  return S == T && (T = (S = buf) + fread(buf, 1, 1 << 21, stdin), S == T)
             ? EOF
             : *S++;
}
template <typename T>
inline void Rdn(T& A) {
  static char ch;
  A = 0, ch = gc();
  while (!isdigit(ch)) ch = gc();
  while (isdigit(ch)) A = (A << 1) + (A << 3) + (ch ^ 48), ch = gc();
}
template <typename T, typename... TT>
inline void Rdn(T& A, TT&... B) {
  return Rdn(A), Rdn(B...);
}
int q, n, m, c, c0, t[N], a[N], b[N], id[N];
inline bool cmp(int A, int B) { return t[A] < t[B]; }
map<int, int> s;
int main() {
  Rdn(q);
  while (q--) {
    Rdn(n, m, c, c0), s.clear();
    for (register int i = (1); i <= (n); ++i) Rdn(t[i], a[i], b[i]), id[i] = i;
    sort(id + 1, id + n + 1, cmp), t[id[n + 1] = n + 1] = m;
    if (c0 < t[id[1]]) {
      puts("-1");
      continue;
    }
    int rem = 0;
    long long res = 0;
    if (c0 - t[id[1]] > 0) s[0] += (rem = c0 - t[id[1]]);
    bool fl = 0;
    for (register int i = (1); i <= (n); ++i) {
      int dt = t[id[i + 1]] - t[id[i]];
      int cost = b[id[i]], rstm = a[id[i]], times = min(a[id[i]], c - rem);
      rstm -= times, rem += times;
      while (!s.empty() && s.rbegin()->first > cost && rstm) {
        int cst = s.rbegin()->first, tim = s.rbegin()->second;
        if (tim <= rstm) {
          times += tim, rstm -= tim, s.erase(--s.end());
        } else {
          s.rbegin()->second -= rstm, times += rstm, rstm = 0;
        }
      }
      s[cost] += times;
      if (rem < dt) {
        puts("-1");
        fl = 1;
        break;
      }
      rem -= dt;
      while (!s.empty() && dt) {
        int cst = s.begin()->first, tim = s.begin()->second;
        if (tim <= dt) {
          res += 1ll * cst * tim, dt -= tim, s.erase(s.begin());
        } else {
          res += 1ll * dt * cst, s[cst] -= dt, dt = 0;
        }
      }
      if (dt) {
        puts("-1");
        fl = 1;
        break;
      }
    }
    if (fl)
      continue;
    else
      printf("%lld\n", res);
  }
  return 0;
}
