#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18 + 1;
struct Line {
  ll len;
  ll a;
};
bool operator<(const Line& lhs, const Line& rhs) {
  return lhs.a < rhs.a || lhs.a == rhs.a && lhs.len < rhs.len;
};
struct Friend {
  ll t;
  ll a, b;
};
bool operator<(const Friend& lhs, const Friend& rhs) { return lhs.t < rhs.t; };
ll solve() {
  int n;
  ll m, c, c0;
  cin >> n >> m >> c >> c0;
  multiset<Line> ls;
  ls.insert(Line{c0, 0});
  vector<Friend> fs(n);
  for (int i = 0; i < n; i++) {
    cin >> fs[i].t >> fs[i].a >> fs[i].b;
  }
  sort(fs.begin(), fs.end());
  ll res = 0;
  ll lenSum = c0;
  bool isok = true;
  auto spend = [&](ll span) {
    if (ls.empty()) {
      isok = false;
      return;
    }
    lenSum -= span;
    auto it = ls.begin();
    ll st = 0;
    while (it != ls.end()) {
      ll l = it->len;
      st = min(span, l);
      res += it->a * st;
      span -= st;
      if (span == 0) break;
      it++;
    }
    ls.erase(ls.begin(), it);
    if (ls.empty()) {
      isok = false;
      return;
    }
    auto v = *ls.begin();
    ls.erase(ls.begin());
    v.len -= st;
    ls.insert(v);
  };
  auto pour = [&](ll a, ll b) {
    ls.insert(Line{a, b});
    lenSum += a;
  };
  auto leak = [&]() {
    if (ls.empty()) {
      isok = false;
      return;
    }
    auto it = prev(ls.end());
    ll st = 0;
    while (lenSum > c) {
      ll l = it->len;
      st = min(l, lenSum - c);
      lenSum -= st;
      if (lenSum == c) break;
      if (it == ls.begin()) {
        isok = false;
        break;
      }
      it--;
    }
    ls.erase(next(it), ls.end());
    if (ls.empty()) {
      isok = false;
      return;
    }
    auto v = *ls.rbegin();
    ls.erase(ls.find(v));
    v.len -= st;
    ls.insert(v);
  };
  int pret = 0;
  for (auto& f : fs) {
    spend(f.t - pret);
    pret = f.t;
    pour(f.a, f.b);
    leak();
  }
  spend(m - pret);
  return isok ? res : -1LL;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    cout << solve() << endl;
  }
  return 0;
}
