#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ch = char;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll q;
  cin >> q;
  for (ll t = 0; t < q; t++) {
    ll n, ans = 0;
    cin >> n;
    vector<ll> k(n), l(n), r(n);
    vector<pair<ll, ll> > a, b;
    for (ll i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      ans += min(l[i], r[i]);
      if (l[i] > r[i]) {
        a.push_back({k[i], l[i] - r[i]});
      }
      if (l[i] < r[i]) {
        b.push_back({n - k[i], r[i] - l[i]});
      }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    priority_queue<ll> ppq, mpq;
    for (ll i = 0; i < (ll)a.size(); i++) {
      ppq.push(a[i].second);
      while (a[i].first < ppq.size()) {
        ppq.pop();
      }
    }
    while (ppq.size()) {
      ans += ppq.top();
      ppq.pop();
    }
    for (ll i = 0; i < (ll)b.size(); i++) {
      mpq.push(b[i].second);
      while (b[i].first < mpq.size()) {
        mpq.pop();
      }
    }
    while (mpq.size()) {
      ans += mpq.top();
      mpq.pop();
    }
    cout << ans << endl;
  }
}
