#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t-- > 0) {
    int n;
    cin >> n;
    ll ans = 0;
    V<P<int, int> > lcam, rcam;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += (ll)min(l, r);
      if (l > r)
        lcam.emplace_back(k, l - r);
      else {
        if (n == k) continue;
        rcam.emplace_back(n - k, r - l);
      }
    }
    V<V<int> > lll(lcam.size()), rrr(rcam.size());
    int lsz = lcam.size(), rsz = rcam.size();
    for (int i = 0; i < lsz; i++)
      lll[min(lsz - 1, lcam[i].first)].emplace_back(lcam[i].second);
    for (int i = 0; i < rsz; i++)
      rrr[min(rsz - 1, rcam[i].first)].emplace_back(rcam[i].second);
    {
      multiset<int> muls;
      for (int i = lsz - 1; i >= 0; i--) {
        for (int j = 0; j < lll[i].size(); j++) muls.insert(-lll[i][j]);
        if (!muls.empty()) {
          auto it = muls.begin();
          ans += ll(-(*it));
          muls.erase(it);
        }
      }
    }
    {
      multiset<int> muls;
      for (int i = rsz - 1; i >= 0; i--) {
        for (int j = 0; j < rrr[i].size(); j++) muls.insert(-rrr[i][j]);
        if (!muls.empty()) {
          auto it = muls.begin();
          ans += ll(-(*it));
          muls.erase(it);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
