#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
int N, M, C_MX, C_STA;
struct node {
  int t;
  int a;
  int b;
} peo[MAXN];
bool cmp(node x, node y) { return x.t < y.t; }
void solve() {
  long long ans = 0;
  scanf("%d%d%d%d", &N, &M, &C_MX, &C_STA);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d%d", &peo[i].t, &peo[i].a, &peo[i].b);
  }
  peo[++N].t = M;
  peo[N].a = 0;
  peo[N].b = 0;
  if (C_STA >= M) {
    puts("0");
    return;
  }
  sort(peo + 1, peo + 1 + N, cmp);
  map<int, int> mp;
  mp[0] += C_STA;
  int now = C_STA;
  for (int i = 1; i <= N; i++) {
    int need = peo[i].t - peo[i - 1].t;
    while (!mp.empty() && need) {
      int pure = min(need, mp.begin()->second);
      mp.begin()->second -= pure;
      need -= pure;
      now -= pure;
      ans = ans + 1LL * pure * mp.begin()->first;
      if (!mp.begin()->second) {
        mp.erase(mp.begin());
      }
    }
    if (need) {
      puts("-1");
      return;
    }
    int over = min(C_MX - now, peo[i].a);
    now += over;
    while (over < peo[i].a && !mp.empty() && mp.rbegin()->first > peo[i].b) {
      int out = min(mp.rbegin()->second, peo[i].a - over);
      mp.rbegin()->second -= out;
      over += out;
      if (!mp.rbegin()->second) {
        mp.erase(--mp.end());
      }
    }
    mp[peo[i].b] += over;
  }
  printf("%lld\n", ans);
  return;
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    solve();
  }
  return 0;
}
