#include <bits/stdc++.h>
using namespace std;
const int MaxN = 5e5;
const int Inf = 1 << 30;
struct node {
  long long v, w, tim;
} P[MaxN + 5];
bool cmp(node A, node B) { return A.tim == B.tim ? A.w < B.w : A.tim < B.tim; }
map<long long, long long> mp;
map<long long, long long>::iterator Head;
map<long long, long long>::reverse_iterator Tail;
int main() {
  int n, m;
  long long V, ans, now, last, sub;
  int T;
  scanf("%d", &T);
  while (T--) {
    mp.clear();
    scanf("%d %d %I64d %I64d", &n, &m, &V, &now);
    for (int i = 1; i <= n; ++i)
      scanf("%I64d %I64d %I64d", &P[i].tim, &P[i].v, &P[i].w);
    ++n;
    ans = last = 0;
    P[n].tim = m;
    P[n].v = P[n].w = 0;
    sort(P + 1, P + 1 + n, cmp);
    mp[0] = now;
    for (int i = 1; i <= n; ++i) {
      sub = P[i].tim - last;
      last = P[i].tim;
      if (sub > now) {
        ans = -1;
        break;
      }
      while (sub) {
        Head = mp.begin();
        if (sub >= Head->second) {
          sub -= Head->second;
          now -= Head->second;
          ans += (Head->first) * (Head->second);
          mp.erase(Head);
        } else {
          ans += (Head->first) * sub;
          now -= sub;
          mp[Head->first] -= sub;
          sub = 0;
        }
      }
      now += P[i].v;
      mp[P[i].w] += P[i].v;
      sub = now - V;
      while (sub > 0) {
        Tail = mp.rbegin();
        if (sub >= Tail->second) {
          sub -= Tail->second;
          now -= Tail->second;
          mp.erase(Tail->first);
        } else {
          now -= sub;
          mp[Tail->first] -= sub;
          sub = 0;
        }
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
