#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int N = 5e5 + 7;
int Q, n, m, Cap, C0;
struct dat {
  int t, mx, cst;
  dat(int _t = 0, int _mx = 0, int _cst = 0) { t = _t, mx = _mx, cst = _cst; }
  inline bool operator<(const dat &tmp) const { return t < tmp.t; }
} A[N];
long long solve() {
  long long ans = 0;
  map<int, int> mp;
  int now = C0;
  mp[0] = C0;
  for (int i = 1; i <= n; i++) {
    int dis = A[i].t - A[i - 1].t;
    while (dis && !mp.empty()) {
      int mx = min(mp.begin()->second, dis);
      mp.begin()->second -= mx;
      dis -= mx;
      now -= mx;
      ans += 1ll * mp.begin()->first * mx;
      if (!mp.begin()->second) mp.erase(mp.begin());
    }
    if (dis) return -1;
    int New = min(Cap - now, A[i].mx);
    now += New;
    while (New < A[i].mx && !mp.empty() && mp.rbegin()->first > A[i].cst) {
      int mx = min(mp.rbegin()->second, A[i].mx - New);
      mp.rbegin()->second -= mx;
      New += mx;
      if (!mp.rbegin()->second) mp.erase(--mp.end());
    }
    mp[A[i].cst] += New;
  }
  return ans;
}
int main() {
  Q = read();
  while (Q--) {
    n = read(), m = read(), Cap = read(), C0 = read();
    for (int i = 1; i <= n; i++)
      A[i].t = read(), A[i].mx = read(), A[i].cst = read();
    A[++n] = dat(m, 0, 0);
    sort(A + 1, A + n + 1);
    printf("%lld\n", solve());
  }
  return 0;
}
