#include <bits/stdc++.h>
using namespace std;
const int NN = 500100;
pair<int, pair<int, int> > p[NN];
multiset<pair<int, int> > st;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m, c, c0;
    scanf("%d%d%d%d", &n, &m, &c, &c0);
    for (int i = 1; i <= n; i++)
      scanf("%d%d%d", &p[i].first, &p[i].second.second, &p[i].second.first);
    sort(p + 1, p + n + 1);
    st.clear();
    st.insert({0, c0});
    long long ans = 0;
    p[0].first = 0;
    p[n + 1].first = m;
    int sz = c0;
    for (int i = 1; i <= n + 1; i++) {
      int len = p[i].first - p[i - 1].first;
      sz -= len;
      while (1) {
        if (st.empty()) {
          ans = -1;
          break;
        }
        auto it = st.begin();
        pair<int, int> d = *it;
        st.erase(it);
        if (d.second < len) {
          len -= d.second;
          ans += 1LL * d.first * d.second;
          continue;
        }
        ans += 1LL * len * d.first;
        d.second -= len;
        if (d.second) st.insert(d);
        break;
      }
      if (ans == -1) break;
      st.insert(p[i].second);
      sz += p[i].second.second;
      if (sz > c) {
        while (1) {
          auto it = --st.end();
          pair<int, int> d = *it;
          st.erase(it);
          if (sz - d.second > c) {
            sz -= d.second;
            continue;
          }
          d.second -= (sz - c);
          sz = c;
          if (d.second) st.insert(d);
          break;
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
