#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;
void solve() {
  int n;
  cin >> n;
  vi k(n + 1), l(n + 1), r(n + 1);
  vvi a(n + 1), b(n + 1);
  ll ans = 0, cl = 0;
  for (int i = 1; i <= n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    if (l[i] > r[i] || k[i] == n) {
      a[k[i]].push_back(i);
      cl++;
    } else
      b[k[i] + 1].push_back(i);
    ans += min(l[i], r[i]);
  }
  priority_queue<int, vector<int>, greater<int> > pq;
  int ap = cl, bp = cl + 1;
  for (int i = ap + 1; i <= n; i++) {
    for (int j : a[i]) pq.push(max(0, l[j] - r[j]));
  }
  for (int i = ap; i >= 1; i--) {
    for (int j : a[i]) pq.push(max(0, l[j] - r[j]));
    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }
  for (int i = 1; i <= ap; i++)
    for (int j : b[i]) pq.push(r[j] - l[j]);
  for (int i = bp; i <= n; i++) {
    for (int j : b[i]) pq.push(r[j] - l[j]);
    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
