#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<long long> lefters[n], righters[n];
  for (long long i = 0; i < long long(n); i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    k--;
    ans += min(l, r);
    if (l > r) {
      lefters[k].push_back(l - r);
    } else {
      righters[k].push_back(r - l);
    }
  }
  priority_queue<long long> pql, pqr;
  for (long long i = 0; i < long long(n); i++) {
    for (long long val : lefters[i]) pql.push(val);
    while ((long long)pql.size() > i + 1) pql.pop();
    for (long long val : righters[n - i - 1]) pqr.push(val);
    while ((long long)pqr.size() > i + 1) pqr.pop();
  }
  while (!pql.empty()) {
    ans += pql.top();
    pql.pop();
  }
  while (!pqr.empty()) {
    ans += pqr.top();
    pqr.pop();
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
