#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
template <typename T>
inline T read() {
  T ans = 0, flag = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') flag = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    ans = ans * 10 + c - 48;
    c = getchar();
  }
  return ans * flag;
}
struct person {
  int t, a, b;
  person(int t = 0, int a = 0, int b = 0) : a(a), b(b), t(t) {}
  inline bool operator<(const person &B) const { return t < B.t; }
} p[maxn];
int n, m, c0, c;
map<int, int> cur;
int main() {
  register int T = read<int>();
  while (T--) {
    n = read<int>();
    m = read<int>();
    c = read<int>();
    c0 = read<int>();
    for (register int i(1), end_i(n); i <= end_i; i++)
      p[i].t = read<int>(), p[i].a = read<int>(), p[i].b = read<int>();
    p[++n] = person(m, 0, 0);
    sort(p + 1, p + 1 + n);
    long long ans = 0;
    cur.clear();
    cur[0] = c0;
    for (register int i(1), end_i(n); i <= end_i; i++) {
      int delta = p[i].t - p[i - 1].t;
      if (delta > c) {
        ans = -1;
        break;
      }
      while (!cur.empty() && delta) {
        int ccc = min(delta, cur.begin()->second);
        ans += 1ll * ccc * cur.begin()->first;
        delta -= ccc;
        c0 -= ccc;
        cur.begin()->second -= ccc;
        if (!cur.begin()->second) cur.erase(cur.begin());
      }
      if (delta) {
        ans = -1;
        break;
      }
      int Cnt(p[i].a), Cost(p[i].b), ccc(min(c - c0, Cnt));
      c0 += ccc;
      while (!cur.empty() && ccc < Cnt && Cost < cur.rbegin()->first)
        if (Cnt - ccc >= cur.rbegin()->second) {
          ccc += cur.rbegin()->second;
          cur.erase(--cur.end());
        } else {
          cur.rbegin()->second -= Cnt - ccc;
          ccc = Cnt;
        }
      cur[Cost] += ccc;
    }
    cout << ans << '\n';
  }
  return 0;
}
