#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>;
struct UnionFind {
  vector<int> par;
  vector<int> count;
  vector<int> rank;
  function<ll(ll, ll)> nk;
  UnionFind(int N, function<ll(ll, ll)> nkt) {
    count.assign(N, 1);
    rank.assign(N, 0);
    par.assign(N, 0);
    for (ll i = 0; i < (N); i++) par[i] = i;
    nk = nkt;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      par[x] = root(par[x]);
      return par[x];
    }
  }
  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y) return;
    if (rank[x] < rank[y])
      swap(x, y);
    else if (rank[x] == rank[y])
      rank[y]++;
    par[y] = par[x] = nk(par[y], par[x]);
    count[x] += count[y];
    return;
  }
  int size(int x) { return count[root(x)]; }
  bool issame(int x, int y) { return root(x) == root(y); }
};
using UF = struct UnionFind;
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;
  UF ul(N + 1, [](ll a, ll b) { return (a < b ? a : b); }),
      ur(N + 1, [](ll a, ll b) { return (a > b ? a : b); });
  while (!pq.empty()) {
    auto [k, l, r, d] = pq.top();
    pq.pop();
    if (l > r) {
      if (ul.root(k + 1) == 0) {
        ur.unite(k, ur.par[k] + 1);
        ans += r;
      } else {
        ul.unite(k + 1, ul.par[k + 1] - 1);
        ans += l;
      }
    } else {
      if (ur.root(k) == N or k == N - 1) {
        ul.unite(k + 1, ul.par[k + 1] - 1);
        ans += l;
      } else {
        ur.unite(k, ur.par[k] + 1);
        ans += r;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  init();
  ll T;
  cin >> T;
  for (ll _ = 0; _ < (T); _++) solve();
}
