#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.K < p2.K; });
  sort(lp2.begin(), lp2.end(), [](P p1, P p2) { return p1.K > p2.K; });
  ll lv = 1;
  for (auto &p : lp1) {
    if (lv <= p.K) {
      ans += p.L;
      lv++;
    } else {
      ans += p.R;
    }
  }
  ll rv = N;
  for (auto &p : lp2) {
    if (rv > p.K) {
      ans += p.R;
      rv--;
    } else {
      ans += p.L;
    }
  }
  print(ans);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll T;
  cin >> T;
  while (T--) solve();
}
