#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1LL << 60;
const long long mod = 1e9 + 7;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
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;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> k(n), l(n), r(n);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    ans += min(l[i], r[i]);
  }
  vector<long long> ids(n);
  iota(ids.begin(), ids.end(), 0);
  sort(ids.begin(), ids.end(),
       [&](long long i, long long j) { return k[i] < k[j]; });
  priority_queue<long long> pque;
  for (long long i = n - 1; i >= 0; i--) {
    long long j = ids[i];
    if (l[j] <= r[j]) {
      long long x = r[j] - l[j];
      if (n - k[j] > (long long)pque.size()) {
        pque.push(x);
      } else {
        if (pque.empty()) continue;
        long long y = pque.top();
        if (y < x) {
          pque.pop();
          pque.push(x);
        }
      }
    }
  }
  while (pque.size()) {
    ans += pque.top();
    pque.pop();
  }
  priority_queue<long long> pque1;
  for (long long i = 0; i < n; i++) {
    long long j = ids[i];
    if (l[j] > r[j]) {
      long long x = l[j] - r[j];
      if (k[j] > (long long)pque1.size()) {
        pque1.push(x);
      } else {
        if (pque1.empty()) continue;
        long long y = pque1.top();
        if (y < x) {
          pque1.pop();
          pque1.push(x);
        }
      }
    }
  }
  while (pque1.size()) {
    ans += pque1.top();
    pque1.pop();
  }
  cout << ans << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) solve();
  return 0;
}
