#include <bits/stdc++.h>
using namespace std;
const long long maxn = 500500;
long long n, m, c, c0;
long long t[maxn], a[maxn], b[maxn];
long long I[maxn];
long long ans;
bool cmp(long long i, long long j) { return t[i] < t[j]; }
long long solve() {
  cin >> n >> m >> c >> c0;
  for (long long i = 0; i < n; i++) cin >> t[i] >> a[i] >> b[i];
  t[n] = 0, a[n] = c0, b[n] = 0;
  n++;
  t[n] = m, a[n] = 0, b[n] = 0;
  for (long long i = 0; i <= n; i++) I[i] = i;
  sort(I, I + n + 1, cmp);
  map<long long, long long> buk;
  long long tot = 0;
  ans = 0;
  for (long long _ = 0; _ < n; _++) {
    long long i = I[_], j = I[_ + 1];
    {
      long long tmp = min(c - tot, a[i]);
      buk[b[i]] += tmp;
      a[i] -= tmp;
      tot += tmp;
      while (buk.size() and a[i]) {
        long long B = buk.rbegin()->first;
        if (B <= b[i]) break;
        long long pmt = min(a[i], buk[B]);
        buk[B] -= pmt;
        if (buk[B] == 0) buk.erase(B);
        buk[b[i]] += pmt;
        a[i] -= pmt;
      }
    }
    long long d = t[j] - t[i];
    if (d > tot) return -1;
    tot -= d;
    while (d and buk.size()) {
      long long B = buk.begin()->first;
      long long tmp = min(buk[B], d);
      d -= tmp;
      buk[B] -= tmp;
      if (buk[B] == 0) buk.erase(B);
      ans += tmp * B;
    }
    assert(d == 0);
  }
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  for (; t--;) cout << solve() << "\n";
  return 0;
}
