#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, ll>;
int main() {
  int t;
  cin >> t;
  for (int _ = 0; _ < t; ++_) {
    int n;
    cin >> n;
    ll ans = 0;
    vector<P> vl, vr;
    for (int i = 0; i < n; ++i) {
      ll k, l, r;
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l > r) vl.emplace_back(k, l - r);
      if (l < r) vr.emplace_back(n - k, r - l);
    }
    sort(vl.rbegin(), vl.rend());
    sort(vr.rbegin(), vr.rend());
    int i = 0;
    priority_queue<ll> q;
    for (int _k = 0; _k < int(vl.size()); ++_k) {
      int k = vl.size() - _k;
      if (i < vl.size() && vl[i].first >= k) q.emplace(vl[i++].second);
      if (!q.empty()) ans += q.top(), q.pop();
    }
    i = 0;
    q = priority_queue<ll>();
    for (int _k = 0; _k < int(vr.size()); ++_k) {
      int k = vr.size() - _k;
      if (i < vr.size() && vr[i].first >= k) q.emplace(vr[i++].second);
      if (!q.empty()) ans += q.top(), q.pop();
    }
    cout << ans << endl;
  }
}
