#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 10;
typedef long long ll;

int n;
vector<int> wl[maxN], wr[maxN];

void process() {
  ll ans = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l >= r) {
      wl[k].push_back(l-r);
    } else {
      wr[n-k].push_back(r-l);
    }
    ans = ans + min(l, r);
  }
  priority_queue<int, vector<int>, greater<int> > pq;
  for (int i = 1; i <= n; ++i) {
    for (auto v : wl[i]) {
      pq.push(v);
      ans += v;
    }
    while (pq.size() > i) {
      int x = pq.top(); pq.pop();
      ans -= x;
    }
  }
  while (!pq.empty()) pq.pop();
  for (int i = 1; i <= n; ++i) {
    for (auto v : wr[i]) {
      pq.push(v);
      ans += v;
    }
    while (pq.size() > i) {
      int x = pq.top(); pq.pop();
      ans -= x;
    }
  }
  cout << ans << '\n';
  for (int i = 1; i <= n; ++i) {
    wl[i].clear(); wr[i].clear();
  }
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);
  int t; cin >> t;
  while (t--) process();
  return 0;
}
