#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vector<T>>;
template <class T>
inline bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
using arr = array<ll, 4>;
template <typename T>
struct BIT {
  ll n;
  vector<T> d;
  BIT(ll n = 0) : n(n), d(n + 1) {}
  void add(ll i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(ll i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};
void solve() {
  ll N;
  cin >> N;
  vc<arr> A(N);
  for (ll i = 0; i < (N); i++) {
    cin >> A[i][0] >> A[i][1] >> A[i][2];
    A[i][0]--;
    A[i][3] = abs(A[i][1] - A[i][2]);
  }
  auto comp = [&](arr a1, arr a2) { return a1[3] < a2[3]; };
  priority_queue<arr, vc<arr>, decltype(comp)> pq{comp};
  for (ll i = 0; i < (N); i++) pq.emplace(A[i]);
  ll ans = 0;
  BIT<ll> L(N), R(N + 1);
  for (ll i = 0; i < (N - 1); i++) L.add(i + 1), R.add(i + 1);
  while (!pq.empty()) {
    auto [k, l, r, d] = pq.top();
    pq.pop();
    if (l > r) {
      if (L.sum(k) < 0) {
        R.add(N - k - 1, -1);
        ans += r;
      } else {
        L.add(k, -1);
        ans += l;
      }
    } else {
      if (R.sum(N - k - 1) < 1) {
        L.add(k, -1);
        ans += l;
      } else {
        R.add(N - k - 1, -1);
        ans += r;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  init();
  ll T;
  cin >> T;
  for (ll _ = 0; _ < (T); _++) solve();
}
