#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline void Yes(bool condition) {
  if (condition)
    cout << "Yes"
         << "\n";
  else
    cout << "No"
         << "\n";
}
template <class itr>
void cins(itr first, itr last) {
  for (auto i = first; i != last; i++) {
    cin >> (*i);
  }
}
template <class itr>
void array_output(itr start, itr goal) {
  string ans = "", k = " ";
  for (auto i = start; i != goal; i++) ans += to_string(*i) + k;
  if (!ans.empty()) ans.pop_back();
  cout << ans << "\n";
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
const ll INF = 1e15;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll MAX = 200005;
constexpr ll nx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr ll ny[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll t;
  cin >> t;
  for (ll _ = ll(0); _ < ll(t); ++_) {
    ll n;
    cin >> n;
    vector<ll> a(n + 1, 0), k(n), l(n), r(n);
    for (ll i = ll(0); i < ll(n); ++i) cin >> k[i] >> l[i] >> r[i];
    ll ans = 0;
    vector<vector<ll>> p(n + 1), m(n + 1);
    for (ll i = ll(0); i < ll(n); ++i) {
      ans += min(l[i], r[i]);
      if (l[i] > r[i]) {
        p[k[i]].push_back(l[i] - r[i]);
      }
      if (l[i] < r[i]) {
        p[n - k[i]].push_back(r[i] - l[i]);
      }
    }
    priority_queue<ll, vector<ll>, greater<ll>> q;
    for (ll i = ll(1); i < ll(n + 1); ++i) {
      for (ll s : p[i]) q.push(s);
      while (q.size() > i) {
        q.pop();
      }
    }
    ll d = q.size();
    for (ll i = ll(0); i < ll(d); ++i) {
      ans += q.top();
      q.pop();
    }
    for (ll i = ll(1); i < ll(n + 1); ++i) {
      for (ll s : m[i]) q.push(s);
      while (q.size() > i) {
        q.pop();
      }
    }
    d = q.size();
    for (ll i = ll(0); i < ll(d); ++i) {
      ans += q.top();
      q.pop();
    }
    cout << ans << "\n";
  }
}
