#include <bits/stdc++.h>
using namespace std;
int test;
int n;
long long m, c, c0, c1;
set<pair<long long, long long> > s;
vector<pair<long long, pair<long long, long long> > > fr;
long long ans = 0;
void inserting(pair<long long, long long> p) {
  auto itf = s.lower_bound({p.first, 0});
  if (itf == s.end() || (itf->first) > p.first)
    s.insert(p);
  else {
    long long aval = itf->second;
    s.erase(itf);
    s.insert({p.first, p.second + aval});
  }
}
void play(long long t) {
  if (c0 >= t) {
    c0 -= t;
    return;
  } else {
    t -= c0;
    c0 = 0;
  }
  auto it = s.begin();
  while (1) {
    if (it == s.end()) {
      ans = -1;
      return;
    }
    pair<long long, long long> f = *it;
    if (t <= f.second) {
      ans += (t * f.first);
      c1 -= t;
      s.erase(it);
      if (t != f.second) inserting({f.first, f.second - t});
      break;
    }
    ans += (f.second * f.first);
    c1 -= f.second;
    t -= f.second;
    s.erase(it++);
  }
}
void addfr(pair<long long, long long> f) {
  inserting(f);
  c1 += f.second;
  if (s.empty()) return;
  if (c1 <= c - c0) return;
  auto it = s.end();
  it--;
  while (1) {
    pair<long long, long long> p = *it;
    if (c1 - p.second <= c - c0) {
      s.erase(it);
      if (c1 - p.second != c - c0)
        inserting({p.first, c - c0 - (c1 - p.second)});
      c1 = c - c0;
      break;
    }
    c1 -= p.second;
    s.erase(it--);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> test;
  for (int tt = 1; tt <= test; tt++) {
    cin >> n >> m >> c >> c0;
    s.clear();
    fr.resize(n);
    c1 = 0;
    ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> fr[i].first >> fr[i].second.second >> fr[i].second.first;
    }
    sort(fr.begin(), fr.end());
    for (int i = 0; i < n; i++) {
      if (i == 0)
        play(fr[i].first);
      else
        play(fr[i].first - fr[i - 1].first);
      if (ans == -1) {
        cout << -1 << endl;
        break;
      }
      addfr(fr[i].second);
    }
    if (ans == -1) continue;
    if (n == 0)
      play(m);
    else
      play(m - fr[n - 1].first);
    cout << ans << endl;
  }
  return 0;
}
