#include <bits/stdc++.h>
using namespace std;
struct no {
  long long t, x, y;
} a[1000010];
long long operator<(no x, no y) { return x.t < y.t; }
long long n, m, c, c0;
void sol() {
  long long r = 0;
  cin >> n >> m >> c >> c0;
  for (long long i = 1; i <= n; i++)
    scanf("%I64d%I64d%I64d", &a[i].t, &a[i].x, &a[i].y);
  a[++n] = (no){m, 0, 0};
  sort(a + 1, a + n + 1);
  map<long long, long long> ma;
  ma[0] = c0;
  for (long long i = 1; i <= n; i++) {
    long long nt = a[i].t - a[i - 1].t;
    if (nt > c) {
      r = -1;
      break;
    }
    while (!ma.empty() && nt > 0) {
      long long v = min(nt, ma.begin()->second);
      r += ma.begin()->first * v;
      ma.begin()->second -= v;
      nt -= v;
      c0 -= v;
      if (!ma.begin()->second) ma.erase(ma.begin());
    }
    if (nt) {
      r = -1;
      break;
    }
    long long c1 = a[i].x, c2 = a[i].y, v = min(c - c0, a[i].x);
    c0 += v;
    while (!ma.empty() && v < c1 && c2 < ma.rbegin()->first) {
      if (c1 - v >= ma.rbegin()->second) {
        v += ma.rbegin()->second;
        ma.erase(--ma.end());
      } else {
        ma.rbegin()->second -= c1 - v;
        v = c1;
      }
    }
    ma[c2] += v;
  }
  cout << r << '\n';
}
signed main() {
  long long t;
  cin >> t;
  while (t--) sol();
}
