#include <bits/stdc++.h>
using namespace std;
const int64_t INF = (1 << 30) - 1;
const long long oo = (1ll << 62) - 1;
const long double PI = 3.1415926535898;
const int64_t N = 5 * 100000 + 5;
int64_t q;
int64_t n, m, c, c0;
pair<int64_t, pair<int64_t, int64_t> > a[N];
multiset<pair<int64_t, int64_t> > ms;
void solve() {
  cin >> n >> m >> c >> c0;
  for (int64_t i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second.first >> a[i].second.second;
  }
  a[n + 1] = make_pair(0, make_pair(0, 0));
  if (n == 0) {
    if (c0 >= m) {
      cout << 0 << '\n';
    } else
      cout << -1 << '\n';
    return;
  }
  ms.clear();
  a[0] = make_pair(0, make_pair(c0, 0));
  sort(a + 1, a + n + 1);
  int64_t res = 0;
  int64_t sum = 0;
  for (int64_t i = 0; i <= n; i++) {
    ms.insert(make_pair(a[i].second.second, a[i].second.first));
    res += a[i].second.second * a[i].second.first;
    sum += a[i].second.first;
    if (a[i + 1].first == a[i].first) continue;
    while (sum > c && ms.size()) {
      auto it = ms.end();
      it--;
      ms.erase(it);
      sum -= it->second;
      res -= it->first * it->second;
      if (sum < c) {
        int64_t cnt = c - sum;
        ms.insert(make_pair(it->first, cnt));
        res += cnt * it->first;
        sum += cnt;
      }
    }
    int64_t kc = (i == n ? m : a[i + 1].first) - a[i].first;
    if (sum < kc) {
      cout << -1 << '\n';
      return;
    }
    while (kc > 0 && ms.size()) {
      auto it = ms.begin();
      ms.erase(it);
      sum -= it->second;
      kc -= it->second;
      if (kc < 0) {
        sum -= kc;
        ms.insert(make_pair(it->first, -kc));
        kc = 0;
      }
    }
  }
  for (auto it : ms) res -= it.first * it.second;
  cout << res << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  for (int64_t i = 1; i <= q; i++) solve();
}
