#include <bits/stdc++.h>
using namespace std;
long long solve() {
  struct tab {
    int t, a, b;
  };
  int n, m, c, c0;
  cin >> n >> m >> c >> c0;
  vector<tab> friends(n);
  for (int i = 0; i != n; ++i)
    cin >> friends[i].t >> friends[i].a >> friends[i].b;
  sort(friends.begin(), friends.end(),
       [](const tab& x, const tab& y) -> bool { return x.t < y.t; });
  map<int, int> water;
  water[0] = c0;
  long long cost = 0;
  friends.push_back(tab({m, 0, 0}));
  for (int i = 0, tim = 0, vol = c0; i <= n; ++i) {
    int t = friends[i].t, a = friends[i].a, b = friends[i].b;
    int need = t - tim;
    tim = t;
    if (vol < need) return -1;
    vol -= need;
    while (water.begin()->second < need) {
      cost += 1LL * water.begin()->first * water.begin()->second;
      need -= water.begin()->second;
      water.erase(water.begin());
    }
    cost += 1LL * water.begin()->first * need;
    if (!(water.begin()->second -= need)) water.erase(water.begin());
    vol += a;
    water[b] += a;
    if (vol > c) {
      need = vol - c;
      vol = c;
      while (prev(water.end())->second <= need) {
        need -= prev(water.end())->second;
        water.erase(prev(water.end()));
      }
      prev(water.end())->second -= need;
    }
  }
  return cost;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T;
  cin >> T;
  while (T--) cout << solve() << "\n";
}
