#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
void solve() {
  int N;
  cin >> N;
  priority_queue<int> pql, pqr;
  ll ans = 0;
  vector<pair<int, ll>> p, q;
  for (int i = (int)(0); i < (int)(N); ++i) {
    int k;
    ll l, r;
    cin >> k >> l >> r;
    ans += min(l, r);
    if (l > r)
      p.push_back({k, l - r});
    else
      q.push_back({N - k, r - l});
  }
  sort(p.begin(), p.end());
  sort(q.begin(), q.end());
  int cnt = 0;
  for (auto pp : p) {
    ll fi = pp.first, se = pp.second;
    cnt++;
    pql.push(-se);
    ans += se;
    if (cnt > fi) {
      ans += pql.top();
      pql.pop();
    }
  }
  cnt = 0;
  for (auto qq : q) {
    ll fi = qq.first, se = qq.second;
    cnt++;
    pqr.push(-se);
    ans += se;
    if (cnt > fi) {
      ans += pqr.top();
      pqr.pop();
    }
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
}
