#include <bits/stdc++.h>
using namespace std;
const int N = 550000;
struct Data {
  int t, a, b;
  Data(int t = 0, int a = 0, int b = 0) : t(t), a(a), b(b) {}
  bool operator<(const Data &d) const { return b < d.b; }
} P[N];
multiset<Data> S;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int tcase;
  cin >> tcase;
  while (tcase--) {
    int n, m, c, c0;
    cin >> n >> m >> c >> c0;
    for (int i = 1; i <= n; i++) cin >> P[i].t >> P[i].a >> P[i].b;
    auto solve = [&]() {
      long long ret = 0, tot_len = 0;
      sort(P + 1, P + n + 1,
           [&](const Data &u, const Data &v) { return u.t < v.t; });
      P[0] = Data(0, c0, 0), P[n + 1] = Data(m, 0, 0);
      S.clear();
      S.insert(P[0]), tot_len += P[0].a;
      for (int i = 1; i <= n + 1; i++) {
        long long len = P[i].t - P[i - 1].t;
        while (len) {
          if (!S.size()) return -1LL;
          auto cur = *S.begin();
          S.erase(S.begin()), tot_len -= cur.a;
          if (cur.a > len)
            cur.a -= len, S.insert(cur), tot_len += cur.a,
                ret += (long long)len * cur.b, len = 0;
          else
            ret += (long long)cur.a * cur.b, len -= cur.a;
        }
        S.insert(P[i]), tot_len += P[i].a;
        while (tot_len > c) {
          auto it = S.end();
          it--;
          auto cur = *it;
          S.erase(it), tot_len -= cur.a;
          if (tot_len < c) {
            cur.a = c - tot_len, S.insert(cur), tot_len += cur.a;
          }
        }
      }
      return ret;
    };
    cout << solve() << "\n";
  }
  return 0;
}
