#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 2e5 + 5;
void solve() {
  long long n;
  cin >> n;
  multiset<pair<long long, long long> > s, neg;
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (l > r)
      s.insert({k, r - l}), ans += r;
    else
      neg.insert({n - k, l - r}), ans += l;
  }
  multiset<long long> taken;
  long long cnt = 0;
  while ((long long)((s).size())) {
    auto x = *s.begin();
    s.erase(s.begin());
    if ((long long)((taken).size()) < x.first) {
      taken.insert(x.second);
    } else {
      auto it = taken.upper_bound(x.second);
      if (it != taken.end()) {
        taken.erase(it);
        taken.insert(x.second);
      }
    }
    cnt++;
  }
  for (long long x : taken) ans -= x;
  taken.clear();
  while ((long long)((neg).size())) {
    auto x = *neg.begin();
    neg.erase(neg.begin());
    if ((long long)((taken).size()) < x.first) {
      taken.insert(x.second);
    } else {
      auto it = taken.upper_bound(x.second);
      if (it != taken.end()) {
        taken.erase(it);
        taken.insert(x.second);
      }
    }
  }
  for (long long x : taken) ans -= x;
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
