#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = vector<vector<T>>;
constexpr ll mod = 1000000007;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 + 99);
const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T, typename U>
inline bool chmin(T& t, const U& u) {
  if (t > u) {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T, typename U>
inline bool chmax(T& t, const U& u) {
  if (t < u) {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T>
inline T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T, typename Y>
inline T mpow(T a, Y n) {
  T res = 1;
  for (; n; n >>= 1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}
template <typename T, typename Y>
ostream& operator<<(ostream& os, const pair<T, Y>& p) {
  return os << "{" << p.first << "," << p.second << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const V<T>& v) {
  os << "{";
  for (auto e : v) os << e << ",";
  return os << "}";
}
template <typename... Args>
void debug(Args&... args) {
  for (auto const& x : {args...}) {
    cerr << x << ' ';
  }
  cerr << '\n';
}
template <typename T>
struct BIT {
  int n;
  vector<T> bit;
  BIT() : n(-1) {}
  BIT(int n_, T d) : n(n_), bit(n_ + 1, d) {}
  T sum(int i) {
    T s = bit[0];
    for (int x = i; x > 0; x -= (x & -x)) s += bit[x];
    return s;
  }
  void add(int i, T a) {
    if (i == 0) return;
    for (int x = i; x <= n; x += (x & -x)) bit[x] += a;
  }
  T lower_bound(T w) {
    if (w <= 0) return 0;
    T x = 0, r = 1;
    while (r < n) r <<= 1;
    for (T k = r; k > 0; k >>= 1) {
      if (x + k <= n && bit[x + k] < w) {
        w -= bit[x + k];
        x += k;
      }
    }
    return x + 1;
  }
  T sum0(int i) { return sum(i + 1); }
  void add0(int i, T a) { add(i + 1, a); }
  T query(int l, int r) { return sum(r - 1) - sum(l - 1); }
  T query0(int l, int r) { return sum(r) - sum(l); }
};
signed main() {
  cin.tie(0);
  cerr.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int qq;
  cin >> qq;
  while (qq--) {
    ll n;
    cin >> n;
    V<tuple<ll, ll, ll, ll>> v(n);
    for (ll i = 0; i < (ll)(n); ++i) {
      ll k, l, r;
      cin >> k >> l >> r;
      v[i] = make_tuple(abs(l - r), k, l, r);
    }
    sort((v).begin(), (v).end());
    reverse((v).begin(), (v).end());
    BIT<ll> lse(n + 2, 0), rse(n + 2, 0);
    ll ans = 0;
    for (ll i = 0; i < (ll)(n); ++i) {
      ll k, l, r, tmp;
      tie(tmp, k, l, r) = v[i];
      if (l > r) {
        if (lse.query(0, k + 1) == k) {
          ans += r;
        } else {
          lse.add(k, 1);
          ans += l;
        }
      } else {
        if (rse.query(k + 1, n + 1) == n - k) {
          ans += l;
        } else {
          rse.add(k + 1, 1);
          ans += r;
        }
      }
    }
    cout << ans << '\n';
  }
}
