#include <bits/stdc++.h>
using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
class myComp {
 public:
  bool operator()(vector<long long> a, vector<long long> b) {
    if (a[0] == b[0]) {
      return a[1] > b[1];
    }
    return a[0] < b[0];
  }
};
class myComp2 {
 public:
  bool operator()(vector<long long> a, vector<long long> b) {
    if (a[0] == b[0]) {
      return a[2] > b[2];
    }
    return a[0] > b[0];
  }
};
int main() {
  long long test;
  cin >> test;
  for (long long tst = 1; tst <= test; ++tst) {
    long long n;
    cin >> n;
    vector<vector<long long>> cam(n, vector<long long>(3, 0));
    for (long long i = 0; i <= n - 1; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      cam[i] = {k, l, r};
    }
    debug() << " ["
            << "cam"
               ": "
            << (cam) << "] ";
    vector<vector<long long>> frnt, bck;
    for (auto i : cam) {
      if (i[1] > i[2])
        frnt.push_back(i);
      else
        bck.push_back(i);
    }
    sort(frnt.begin(), frnt.end(), myComp());
    debug() << " ["
            << "frnt"
               ": "
            << (frnt) << "] ";
    vector<vector<long long>> ansf;
    for (auto i : frnt) {
      if (ansf.size() < i[0])
        ansf.push_back(i);
      else
        bck.push_back(i);
    }
    debug() << " ["
            << "ansf"
               ": "
            << (ansf) << "] ";
    sort(bck.begin(), bck.end(), myComp2());
    debug() << " ["
            << "bck"
               ": "
            << (bck) << "] ";
    vector<vector<long long>> ans = ansf;
    for (long long i = 0; i <= (long long)bck.size() - 1; i++)
      ans.push_back(bck[i]);
    long long hap = 0;
    for (long long i = 0; i <= (long long)ans.size() - 1; i++) {
      if (i + 1 <= ans[i][0])
        hap += ans[i][1];
      else
        hap += ans[i][2];
    }
    debug() << " ["
            << "ans"
               ": "
            << (ans) << "] ";
    cout << hap << '\n';
  }
  return 0;
}
