#include <bits/stdc++.h>
using namespace std;
void _co() { cerr << '\n'; }
template <typename T, typename... Ts>
void _co(const T& value, const Ts&... values) {
  cerr << ' ' << value;
  _co(values...);
}
template <typename T, typename... Ts>
void co(const T& value, const Ts&... values) {
  cerr << "deb: " << value;
  _co(values...);
}
struct Data {
  long long time, amo, cost;
  Data(long long _time, long long _amo, long long _cost)
      : time(_time), amo(_amo), cost(_cost){};
  bool operator<(const Data& that) const { return cost < that.cost; }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    vector<Data> a;
    vector<long long> all;
    long long n, m, c, c0;
    cin >> n >> m >> c >> c0;
    for (long long i = (1); i <= (n); i++) {
      long long x, y, z;
      cin >> x >> y >> z;
      a.push_back({x, y, z});
      all.push_back(x);
    }
    all.push_back(m - 1);
    all.push_back(0);
    sort(all.begin(), all.end());
    all.resize(distance(all.begin(), unique(all.begin(), all.end())));
    a.push_back({0, c0, 0});
    sort(a.begin(), a.end(), [&](Data& X, Data& Y) { return X.time < Y.time; });
    multiset<Data> mst;
    long long tInAll = 0, iter = 0, inTank = 0, ans = 0;
    bool cant = false;
    while (tInAll < all.size()) {
      for (; iter < a.size() && a[iter].time <= all[tInAll]; iter++) {
        mst.insert(a[iter]);
        inTank += a[iter].amo;
      }
      while (inTank > c) {
        auto cur = *mst.rbegin();
        mst.erase((--mst.end()));
        long long rem = min(inTank - c, cur.amo);
        cur.amo -= rem;
        inTank -= rem;
        if (cur.amo) mst.insert(cur);
      }
      long long need = [&] {
        if (tInAll + 1 == all.size()) return 1LL;
        return all[tInAll + 1] - all[tInAll];
      }();
      while (need > 0) {
        if (mst.empty()) {
          cant = true;
          break;
        }
        auto cur = *mst.begin();
        mst.erase(mst.begin());
        long long buy = min(cur.amo, need);
        need -= buy;
        cur.amo -= buy;
        inTank -= buy;
        ans += buy * cur.cost;
        if (cur.amo) mst.insert(cur);
      }
      tInAll++;
    }
    if (cant)
      cout << -1 << '\n';
    else
      cout << ans << '\n';
  }
}
