#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<ll, ll>;
ll f(vector<p>& pp) {
  for (auto& elm : pp) {
    elm.first = min(elm.first, (ll)(pp.size() - 1));
  }
  vector<vector<ll> > s(pp.size() + 1);
  for (auto& elm : pp) {
    s[elm.first].push_back(elm.second);
  }
  ll res = 0;
  priority_queue<ll> q;
  for (int i = pp.size() - 1; i >= 0; i--) {
    for (ll x : s[i]) q.push(x);
    if (!q.empty()) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}
void solve() {
  int N;
  cin >> N;
  cin.ignore();
  ll ans = 0;
  vector<p> lp, rp;
  for (int i = 0; i < N; i++) {
    ll k, l, r;
    cin >> k >> l >> r;
    cin.ignore();
    ll m = min(l, r);
    ans += m;
    l -= m;
    r -= m;
    if (l > 0 || k == N) {
      lp.emplace_back(k - 1, l);
    } else {
      rp.emplace_back(N - k, r);
    }
  }
  ans += f(lp);
  ans += f(rp);
  cout << ans << endl;
  return;
}
int main() {
  int T;
  cin >> T;
  cin.ignore();
  for (int i = 0; i < T; i++) {
    solve();
  }
  return 0;
}
