#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
int main() {
  int T;
  cin >> T;
  for (int ti = 0; ti < (T); ++ti) {
    int N;
    cin >> N;
    vector<P> mae, ato;
    int Ki, Li, Ri;
    ll ans = 0;
    for (int i = 0; i < (N); ++i) {
      cin >> Ki >> Li >> Ri;
      if (Li > Ri) {
        mae.push_back(P(Ki, Li - Ri));
      } else {
        ato.push_back(P(N - Ki, Ri - Li));
      }
      ans += min(Li, Ri);
    }
    sort(mae.begin(), mae.end());
    sort(ato.begin(), ato.end());
    if (mae.size() > 0) {
      vector<P> mae_ok;
      int idx = 0;
      for (int i = 1; i <= N; ++i) {
        while (mae[idx].first <= i) {
          if (mae[idx].first == i) mae_ok.push_back(mae[idx]);
          idx++;
        }
        sort(mae_ok.begin(), mae_ok.end(),
             [](const P& a, const P& b) { return a.second > b.second; });
        while (mae_ok.size() > i) mae_ok.pop_back();
        if (idx >= mae.size()) break;
      }
      for (P okp : mae_ok) {
        ans += okp.second;
      }
    }
    if (ato.size() > 0) {
      vector<P> ato_ok;
      int idx = 0;
      for (int i = 1; i <= N; ++i) {
        while (ato[idx].first <= i) {
          if (ato[idx].first == i) ato_ok.push_back(ato[idx]);
          idx++;
        }
        sort(ato_ok.begin(), ato_ok.end(),
             [](const P& a, const P& b) { return a.second > b.second; });
        while (ato_ok.size() > i) ato_ok.pop_back();
        if (idx >= ato.size()) break;
      }
      for (P okp : ato_ok) ans += okp.second;
    }
    cout << ans << endl;
  }
  return 0;
}
