#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
void print() {}
template <class H, class... T>
void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
typedef struct {
  ll K;
  ll L;
  ll R;
} P;
void solve() {
  ll N;
  cin >> N;
  vector<ll> K(N), L(N), R(N);
  for (ll i = (0); i < (N); i++) {
    cin >> K[i] >> L[i] >> R[i];
  }
  ll ans = 0;
  vector<P> lp1, lp2;
  for (ll i = (0); i < (N); i++) {
    if (K[i] == N) {
      ans += L[i];
    } else if (L[i] > R[i]) {
      lp1.push_back({K[i], L[i], R[i]});
    } else {
      lp2.push_back({K[i], L[i], R[i]});
    }
  }
  sort(lp1.begin(), lp1.end(),
       [](P p1, P p2) { return p1.L - p1.R > p2.L - p2.R; });
  sort(lp2.begin(), lp2.end(),
       [](P p1, P p2) { return p1.L - p1.R < p2.L - p2.R; });
  ll lv = 1;
  ll c1 = lp1.size(), c2 = lp2.size();
  vector<bool> check(N + 1, 0);
  for (auto &p : lp1) {
    ll ps = min(p.K, c1);
    bool ok = true;
    while (ps > 0) {
      if (check[ps] == false) {
        check[ps] = true;
        break;
      }
      ps--;
    }
    if (ok) {
      ans += p.L;
    } else {
      ans += p.R;
    }
  }
  for (auto &p : lp2) {
    ll ps = max(p.K + 1, c1 + 1);
    bool ok = true;
    while (ps <= N) {
      if (check[ps] == false) {
        check[ps] = true;
        break;
      }
      ps++;
    }
    if (ok) {
      ans += p.R;
    } else {
      ans += p.L;
    }
  }
  print(ans);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll T;
  cin >> T;
  while (T--) solve();
}
