#include <bits/stdc++.h>
using namespace std;

struct dat {
  long long k, l, r;
  bool operator<(const dat &rd) const {
    if (k != rd.k) return k < rd.k;
    return l > rd.l;
  }
};

long long n;
vector<dat> v, lv, rv;

long long solve();

int main() {
  long long T;
  cin >> T;
  while (T--) {
    cin >> n;
    v.resize(n);
    for (auto &p : v) cin >> p.k >> p.l >> p.r;
    cout << solve() << endl;
  }
  return 0;
}

long long solve() {
  long long res = 0, sum = 0;
  lv.clear();
  rv.clear();
  for (int i = 0; i < n; ++i) {
    sum += v[i].r;
    v[i].l -= v[i].r;
    v[i].r = 0;
    if (v[i].l > 0)
      lv.push_back(v[i]);
    else if (v[i].l != 0)
      rv.push_back(v[i]);
  }
  sort(lv.begin(), lv.end());
  priority_queue<long long, vector<long long>, greater<long long>> pql;
  int len = lv.size();
  for (int i = 0; i < len; ++i) {
    pql.push(lv[i].l);
    while (pql.size() > lv[i].k) pql.pop();
  }
  while (pql.size()) {
    res += pql.top();
    pql.pop();
  }
  priority_queue<long long> pqr;
  sort(rv.begin(), rv.end());
  reverse(rv.begin(), rv.end());
  len = rv.size();
  for (int i = 0; i < len; ++i) {
    rv[i].k = n - rv[i].k;
    pqr.push(rv[i].l);
    while (pqr.size() > rv[i].k) {
      res += pqr.top();
      pqr.pop();
    }
  }
  return res + sum;
}