#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 500500;
struct node {
  int t, a, b;
} a[N];
bool cmp(node a, node b) { return a.t < b.t; }
int n, m, c, C, c0;
long long sum;
void solve() {
  cin >> n >> m >> c >> c0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].t >> a[i].a >> a[i].b;
  }
  ++n;
  a[n].t = m, a[n].a = 0, a[n].b = 0;
  sort(a + 1, a + n + 1, &cmp);
  sum = 0;
  C = c0;
  multiset<pair<long long, long long> > st;
  st.insert(make_pair(0, c0));
  a[0].t = 0;
  for (int i = 1; i <= n; ++i) {
    long long ti = a[i].t - a[i - 1].t;
    C -= ti;
    while (st.size() > 0 && ti > 0) {
      pair<long long, long long> pr = *st.begin();
      st.erase(st.find(pr));
      if (pr.second > ti) {
        st.insert(make_pair(pr.first, pr.second - ti));
        sum += pr.first * ti;
        ti = 0;
      } else {
        ti -= pr.second;
        sum += pr.first * pr.second;
      }
    }
    if (ti > 0) {
      cout << -1 << endl;
      return;
    }
    st.insert(make_pair(a[i].b, a[i].a));
    C += a[i].a;
    while (C > c) {
      pair<long long, long long> pr = *st.rbegin();
      st.erase(st.find(pr));
      if (pr.second > C - c) {
        pr.second -= C - c;
        C = c;
        st.insert(pr);
      } else {
        C -= pr.second;
      }
    }
  }
  cout << sum << endl;
}
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
