#include <bits/stdc++.h>
using namespace std;
multiset<pair<int, int> > q;
int n, m, c, c0, T;
struct note {
  int t, x, v;
} a[500005];
bool cmp(note x, note y) { return x.t < y.t; }
void work() {
  long long ans = 0;
  scanf("%d%d%d%d", &n, &m, &c, &c0);
  int now = c0;
  q.insert(make_pair(0, c0));
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].t, &a[i].x, &a[i].v);
  a[++n] = (note){m, 0, 0};
  std::sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    int res = a[i].t - a[i - 1].t;
    if (now < res) {
      puts("-1");
      return;
    }
    now -= res;
    while (res) {
      auto it = q.begin();
      q.erase(it);
      pair<int, int> t = *it;
      if (res >= t.second)
        res -= t.second, ans += (long long)t.first * t.second;
      else {
        ans += (long long)res * t.first;
        t.second -= res;
        q.insert(t);
        res = 0;
      }
    }
    now += a[i].x, q.insert(make_pair(a[i].v, a[i].x));
    while (now > c) {
      auto it = q.rbegin();
      pair<int, int> t = *it;
      q.erase(--q.end());
      if (now - t.second >= c)
        now -= t.second;
      else {
        t.second -= now - c;
        now = c;
        q.insert(t);
      }
    }
  }
  printf("%lld\n", ans);
}
int main() {
  scanf("%d", &T);
  while (T--) {
    q.clear();
    work();
  }
}
