#include <bits/stdc++.h>
using namespace std;
int const N = 500100;
int n, m, c, c0;
vector<int> mp;
priority_queue<pair<int, int> > q;
struct F {
  int t, a, b, mp;
  F() {}
  void sc() { scanf("%d%d%d", &t, &a, &b); }
  bool operator<(F const &o) const { return t < o.t; }
} fr[N + 1];
struct S {
  long long mx, z;
  S() : mx(0), z(0) {}
  S operator+(S const &o) const {
    S an;
    an.mx = max(mx, o.mx);
    return an;
  }
  void operator+=(int x) {
    mx += x;
    z += x;
  }
} sg[N << 2];
void no() { printf("-1\n"); }
void sl(int id, int a, int b) {
  if (!sg[id].z) return;
  sg[a] += sg[id].z;
  sg[b] += sg[id].z;
  sg[id].z = 0;
}
long long gt(int x, int y, int l = 1, int r = mp.size(), int id = 1) {
  if (r < x || l > y) return 0;
  if (l >= x && r <= y) return sg[id].mx;
  int m = l + r >> 1, a = id << 1, b = a | 1;
  sl(id, a, b);
  return max(gt(x, y, l, m, a), gt(x, y, m + 1, r, b));
}
void ad(int i, int v, int l = 1, int r = mp.size(), int id = 1) {
  if (l == r) return sg[id] += v;
  int m = l + r >> 1, a = id << 1, b = a | 1;
  sl(id, a, b);
  if (i <= m) {
    sg[b] += v;
    ad(i, v, l, m, a);
  } else
    ad(i, v, m + 1, r, b);
  sg[id] = sg[a] + sg[b];
}
void solve() {
  scanf("%d%d%d%d", &n, &m, &c, &c0);
  for (int i = 1; i < (int)(n + 1); ++i) fr[i].sc();
  sort(fr + 1, fr + n + 1);
  fr[0].t = 0;
  fr[0].a = c0;
  fr[0].b = 0;
  fr[n + 1].t = m;
  mp.clear();
  for (int i = 0; i < (int)(n + 2); ++i)
    if (mp.empty() || mp.back() != fr[i].t) mp.push_back(fr[i].t);
  for (int i = 0; i < (int)(n + 2); ++i)
    fr[i].mp = lower_bound(mp.begin(), mp.end(), fr[i].t) - mp.begin() + 1;
  while (!q.empty()) q.pop();
  q.push(make_pair(0, 0));
  long long an = 0;
  for (int i = 1; i < (int)(mp.size() << 2); ++i) sg[i] = S();
  for (int i = 1; i < (int)(mp.size()); ++i) ad(i + 1, -(mp[i] - mp[i - 1]));
  for (int i = 0; i < (int)(n + 1); ++i) {
    int need = fr[i + 1].t - fr[i].t;
    while (need) {
      if (q.empty()) return no();
      int id = q.top().second;
      q.pop();
      int z = min((long long)need,
                  min(c - gt(fr[id].mp, fr[i + 1].mp), (long long)fr[id].a));
      an += (long long)z * fr[id].b;
      fr[id].a -= z;
      ad(fr[id].mp, z);
      if (fr[id].a && c - gt(fr[id].mp, fr[i + 1].mp))
        q.push(make_pair(-fr[id].b, id));
      need -= z;
    }
    q.push(make_pair(-fr[i + 1].b, i + 1));
  }
  printf("%lld\n", an);
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) solve();
}
