#include <bits/stdc++.h>
using namespace std;
template <typename T>
T MAX(T a, T b) {
  return (a > b) ? a : b;
}
template <typename T>
T MIN(T a, T b) {
  return (a < b) ? a : b;
}
template <typename T>
void UMAX(T &a, T b) {
  a = (a > b ? a : b);
}
template <typename T>
void UMIN(T &a, T b) {
  a = (a < b ? a : b);
}
int readint() {
  char c = getchar();
  int ret = 0;
  while (!(c >= '0' && c <= '9')) c = getchar();
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret;
}
void putint(int v) {
  if (!v) {
    putchar('0');
    return;
  }
  if (v >= 10) putint(v / 10);
  putchar('0' + (v % 10));
}
int n, m, c, c0;
struct pnt {
  int t, mx, cst;
  void set(int T, int A, int B) {
    t = T;
    mx = A;
    cst = B;
  }
} pnts[500005];
bool cmp(pnt A, pnt B) { return A.t < B.t; }
struct seg {
  int k;
  mutable int lx;
  void set(int K, int L) {
    k = K;
    lx = L;
  }
  bool operator<(seg B) const { return k < B.k; }
} tmp;
multiset<seg> segs;
void solve() {
  int i, j, k;
  n = readint();
  m = readint();
  c = readint();
  c0 = readint();
  for (i = 0; i < n; ++i) {
    int t, a, b;
    t = readint();
    a = readint();
    b = readint();
    pnts[i].set(t, a, b);
  }
  sort(pnts, pnts + n, cmp);
  pnts[n++].set(m, 0, 0);
  segs.clear();
  tmp.set(0, c0);
  segs.insert(tmp);
  long long dlt = 0ll, tot = (long long)c0;
  int lst = 0;
  for (i = 0; i < n; ++i) {
    int gap = pnts[i].t - lst;
    tot -= (long long)gap;
    if (tot < 0ll) {
      printf("-1\n");
      return;
    }
    while (!segs.empty() && gap >= segs.begin()->lx) {
      dlt += (long long)segs.begin()->lx * (long long)segs.begin()->k;
      gap -= segs.begin()->lx;
      segs.erase(segs.begin());
    }
    if (gap) {
      dlt += (long long)gap * (long long)segs.begin()->k;
      segs.begin()->lx -= gap;
    }
    tmp.set(pnts[i].cst, pnts[i].mx);
    tot += (long long)pnts[i].mx;
    segs.insert(tmp);
    while ((long long)(segs.rbegin()->lx) <= tot - (long long)c) {
      tot -= (long long)segs.rbegin()->lx;
      set<seg>::iterator it = segs.end();
      segs.erase(--it);
    }
    if ((int)tot > c) {
      segs.rbegin()->lx -= (int)tot - c;
      tot = (long long)c;
    }
    lst = pnts[i].t;
  }
  printf("%lld\n", dlt);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
