#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getrnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
ll getrndll(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }
template <typename T1, typename T2>
inline bool relax(T1& a, const T2& b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool strain(T1& a, const T2& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
ll get(int n, vector<pair<int, int>>& a) {
  set<int> cur;
  for (int i = 1, j = 0; i <= n; ++i) {
    if (j == a.size()) break;
    while (j < a.size() && a[j].first <= i) {
      if (a[j].first != 0) cur.insert(a[j].second);
      ++j;
    }
    while (cur.size() > i) cur.erase(cur.begin());
  }
  ll ret = 0;
  for (int i : cur) ret += i;
  return ret;
}
void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  vector<pair<int, int>> pos, neg;
  for (int i = 0; i < n; ++i) {
    int k, l, r;
    cin >> k >> l >> r;
    ans += min(l, r);
    if (l >= r)
      pos.emplace_back(k, l - r);
    else
      neg.emplace_back(n - k, r - l);
  }
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  cout << ans + get(n, pos) + get(n, neg) << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  srand(time(0));
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
