#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vii = V<int>;
using vvll = V<vll>;
using vvii = V<V<int> >;
using PII = P<int, int>;
using PLL = P<ll, ll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
void solve() {
  int n, m, c, c0;
  cin >> n >> m >> c >> c0;
  ll sumall = c0;
  V<P<int, PII> > tab(n);
  for (ll i = 0; i < n; i++) {
    int t, a, b;
    cin >> t >> a >> b;
    tab[i] = make_pair(t, PII(a, b));
    sumall += a;
  }
  sort(tab.begin(), tab.end());
  tab.emplace_back(make_pair(m, PII(0, 0)));
  n++;
  if (sumall < m) {
    cout << -1 << '\n';
    return;
  }
  map<int, int> costs;
  costs[0] = c0;
  int now = c0;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    int t = tab[i].first, a = tab[i].second.first, b = tab[i].second.second;
    int diff = (i == 0 ? t : tab[i].first - tab[i - 1].first);
    while (costs.size() > 0 and diff > 0) {
      int tmp = min(costs.begin()->second, diff);
      ans += ll(costs.begin()->first) * tmp;
      now -= tmp;
      diff -= tmp;
      costs.begin()->second -= tmp;
      if (costs.begin()->second == 0) costs.erase(costs.begin());
    }
    if (diff > 0) {
      cout << -1 << '\n';
      return;
    }
    int inc = min(c - now, a);
    now += inc;
    while (inc < a and costs.size() > 0 and costs.rbegin()->first > b) {
      if (a - inc >= costs.rbegin()->second) {
        inc += costs.rbegin()->second;
        costs.erase(costs.rbegin()->first);
      } else {
        int minus = a - inc;
        costs.rbegin()->second -= minus;
        inc += minus;
      }
    }
    costs[b] += inc;
  }
  cout << ans << '\n';
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q-- > 0) {
    solve();
  }
  return 0;
}
