#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 7;
const double PI = acos(-1);
using namespace std;
const long long mod = 1e6 + 3;
set<long long> libre, izq, der;
long long ponerizq(long long pos);
long long ponerder(long long pos);
long long buscarder(long long pos) {
  auto ite = der.upper_bound(pos);
  if (ite == der.begin()) return -1;
  ite--;
  pos = *ite;
  return ponerder(pos);
  auto nextite = libre.upper_bound(pos);
  if (nextite != libre.end()) {
    der.insert(*nextite);
    libre.erase(nextite);
    return 1;
  }
  return -1;
}
long long buscarizq(long long pos) {
  auto ite = izq.lower_bound(pos);
  if (ite == izq.end()) return -1;
  pos = *ite;
  return ponerizq(pos);
  auto nextite = libre.lower_bound(pos);
  if (nextite == libre.begin()) return -1;
  nextite--;
  izq.insert(*nextite);
  libre.erase(nextite);
  return 1;
}
long long ponerizq(long long pos) {
  auto ite = libre.upper_bound(pos);
  if (ite == libre.begin()) {
    return buscarder(pos);
  }
  ite--;
  izq.insert(*ite);
  libre.erase(ite);
  return 1;
}
long long ponerder(long long pos) {
  auto ite = libre.lower_bound(pos);
  if (ite == libre.end()) {
    return buscarizq(pos);
  }
  der.insert(*ite);
  libre.erase(ite);
  return 1;
}
void solve() {
  long long n;
  cin >> n;
  long long kpos, L, R;
  long long tot = 0;
  vector<pair<long long, pair<long long, long long> > > v;
  libre.clear(), izq.clear(), der.clear();
  for (long long i = 0; i < n; i++) {
    libre.insert(i);
    cin >> kpos >> L >> R;
    kpos--;
    tot += (L + R);
    if (L - R > R - L)
      v.push_back(pair<long long, pair<long long, long long> >(
          L - R, pair<long long, long long>(0, kpos)));
    else
      v.push_back(pair<long long, pair<long long, long long> >(
          R - L, pair<long long, long long>(1, kpos + 1)));
  }
  sort((v).begin(), (v).end());
  long long delta, tipo, pos;
  for (auto par : v) {
    delta = par.first;
    tipo = par.second.first;
    pos = par.second.second;
    if (tipo == 0) {
      tot += ponerizq(pos) * delta;
    } else {
      tot += ponerder(pos) * delta;
    }
  }
  tot /= 2;
  cout << tot << endl;
}
int main() {
  long long casos;
  cin >> casos;
  while (casos--) {
    solve();
  }
}
