#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
const long long INF =
    sizeof(long long) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const long long MOD = (long long)1e9 + 7;
const double EPS = 1e-9;
struct P {
  long long k, l, r;
  P(long long k, long long l, long long r) : k(k), l(l), r(r) {}
  P() : P(0, 0, 0) {}
  bool operator<(const P &other) const { return k < other.k; }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long T;
  cin >> T;
  while (T--) {
    long long N;
    cin >> N;
    vector<P> PV(N);
    for (long long i = (0), i_len = (N); i < i_len; i++) {
      cin >> PV[i].k >> PV[i].l >> PV[i].r;
      PV[i].k;
    }
    sort(begin(PV), end(PV));
    priority_queue<pair<long long, long long> > pq;
    long long sum = 0;
    long long r = N;
    for (long long i = (0), i_len = (N); i < i_len; i++) {
      sum += PV[i].l;
      pq.push(make_pair(PV[i].r - PV[i].l, i));
      if (PV[i].k < ((long long)(pq).size())) {
        auto p = pq.top();
        pq.pop();
        sum += p.first;
        r--;
      }
    }
    while (!pq.empty()) {
      auto p = pq.top();
      pq.pop();
      if (p.first > 0 && PV[p.second].k < r) {
        sum += p.first;
        r--;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
