#include <bits/stdc++.h>
using namespace std;
void add(map<pair<long long, long long>, long long> &mp,
         pair<long long, long long> elem) {
  if (mp.count(elem) == 0) {
    mp.insert({elem, 1});
  } else {
    mp[elem]++;
  }
}
void del(map<pair<long long, long long>, long long> &mp,
         pair<long long, long long> elem) {
  if (mp[elem] == 1) {
    mp.erase(elem);
  } else {
    mp[elem]--;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long q;
  cin >> q;
  for (int k = 0; k < q; ++k) {
    long long n, m, c, c0;
    cin >> n >> m >> c >> c0;
    vector<pair<long long, pair<long long, long long>>> fr;
    fr.push_back({0, {0, c0}});
    fr.push_back({m, {0, 0}});
    for (int i = 0; i < n; ++i) {
      long long a, aa, aaa;
      cin >> a >> aa >> aaa;
      fr.push_back({a, {aaa, aa}});
    }
    sort(fr.begin(), fr.end());
    map<pair<long long, long long>, long long> st;
    long long ans = 0;
    bool bad = false;
    long long sum = 0;
    for (int j = 0; j < fr.size() - 1; ++j) {
      add(st, {fr[j].second.first, fr[j].second.second});
      sum += fr[j].second.second;
      while (sum > c) {
        int val = (*(--st.end())).first.first;
        int cnt = (*(--st.end())).first.second;
        del(st, {val, cnt});
        sum -= cnt;
        if (sum < c) {
          add(st, {val, (c - sum)});
          sum = c;
        }
      }
      long long need = fr[j + 1].first - fr[j].first;
      if (sum < need) {
        cout << -1 << "\n";
        bad = true;
        break;
      }
      while (need != 0) {
        need -= (*st.begin()).first.second;
        ans += (*st.begin()).first.first * (*st.begin()).first.second;
        sum -= (*st.begin()).first.second;
        int val = (*st.begin()).first.first;
        int cnt = (*st.begin()).first.second;
        del(st, {val, cnt});
        if (need < 0) {
          add(st, {val, -need});
          ans -= (-need) * val;
          sum += (-need);
          need = 0;
        }
      }
    }
    if (!bad) cout << ans << "\n";
  }
}
