#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chmax(T &ans, T t) {
  if (t > ans) ans = t;
}
template <class T>
inline void chmin(T &ans, T t) {
  if (t < ans) ans = t;
}
template <class T>
T inf = numeric_limits<T>::max();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long T;
  cin >> T;
  while (T--) {
    long long N;
    cin >> N;
    vector<long long> K(N), L(N), R(N);
    vector<tuple<long long, long long, long long>> KLR(N);
    for (long long i = 0; i < N; i++) {
      cin >> K[i] >> L[i] >> R[i];
      KLR[i] = make_tuple(K[i], L[i], R[i]);
    }
    sort(KLR.begin(), KLR.end());
    vector<tuple<long long, long long, long long>> lv, rv;
    for (auto itr : KLR) {
      long long k, l, r;
      k = get<0>(itr);
      l = get<1>(itr);
      r = get<2>(itr);
      if (l >= r) {
        lv.push_back(itr);
      } else {
        rv.push_back(itr);
      }
    }
    reverse(rv.begin(), rv.end());
    int idx = 1;
    long long ans = 0;
    for (int i = 0; i < lv.size(); ++i) {
      long long k, l, r;
      k = get<0>(lv[i]);
      l = get<1>(lv[i]);
      r = get<2>(lv[i]);
      if (idx <= k) {
        ans += l;
      } else {
        ans += r;
      }
      idx++;
    }
    for (int i = 0; i < rv.size(); ++i) {
      long long k, l, r;
      k = get<0>(rv[i]);
      l = get<1>(rv[i]);
      r = get<2>(rv[i]);
      if (idx <= k) {
        ans += l;
      } else {
        ans += r;
      }
      idx++;
    }
    cout << ans << endl;
  }
}
