#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T>
ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long inf = 1000000001;
const long long INF = (long long)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
vector<long long> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
const long long MOD = 1000000007;
template <typename T1, typename T2>
class LazySegmentTree {
 public:
  using F = function<T1(T1 &, T1 &)>;
  using G = function<T2(T2 &, T2 &)>;
  using H = function<T1(T1 &, T2 &, long long, long long)>;
  long long n;
  vector<T1> node;
  vector<T2> lazy;
  T1 e1;
  T2 e2;
  F query_func;
  G update_func;
  H eval_func;
  LazySegmentTree(vector<T1> a, F query_func, G update_func, H eval_func, T1 e1,
                  T2 e2)
      : query_func(query_func),
        update_func(update_func),
        eval_func(eval_func),
        e1(e1),
        e2(e2) {
    long long _n = a.size();
    n = 1;
    while (n < _n) n *= 2;
    node.resize(2 * n - 1, e1);
    lazy.resize(2 * n - 1, e2);
    for (long long i = 0; i < _n; i++) node[i + n - 1] = a[i];
    for (long long i = n - 2; i >= 0; i--) {
      node[i] = query_func(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }
  inline void eval(long long k, long long l, long long r) {
    if (lazy[k] != e2) {
      node[k] = eval_func(node[k], lazy[k], l, r);
      if (r - l > 1) {
        lazy[2 * k + 1] = update_func(lazy[2 * k + 1], lazy[k]);
        lazy[2 * k + 2] = update_func(lazy[2 * k + 2], lazy[k]);
      }
      lazy[k] = e2;
    }
  }
  void update(long long a, long long b, T2 x, long long k, long long l,
              long long r) {
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = update_func(lazy[k], x);
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = query_func(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  T1 query(long long a, long long b, long long k, long long l, long long r) {
    eval(k, l, r);
    if (b <= l || r <= a) return e1;
    if (a <= l && r <= b) return node[k];
    T1 resl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T1 resr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return query_func(resl, resr);
  }
};
void solve() {
  long long n;
  cin >> n;
  using P = pair<pair<long long, long long>, pair<long long, long long> >;
  vector<P> sklr(n);
  for (long long i = 0; i < (long long)(n); i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    sklr[i] = P(pair<long long, long long>(abs(r - l), k),
                pair<long long, long long>(l, r));
  }
  sort(sklr.begin(), sklr.end());
  reverse(sklr.begin(), sklr.end());
  auto f = [](long long a, long long b) { return a + b; };
  auto g = [](long long a, long long b) { return a + b; };
  auto h = [](long long a, long long b, long long l, long long r) {
    return a + b * (r - l);
  };
  LazySegmentTree<long long, long long> low(vector<long long>(n), f, g, h, 0,
                                            0);
  LazySegmentTree<long long, long long> high(vector<long long>(n), f, g, h, 0,
                                             0);
  long long ans = 0;
  for (long long i = 0; i < (long long)(n); i++) {
    long long k = sklr[i].first.second;
    long long l = sklr[i].second.first, r = sklr[i].second.second;
    long long score = 0;
    if (l > r) {
      long long num = low.query(k - 1, k, 0, 0, low.n);
      if (num == k)
        score = r;
      else {
        score = l;
        low.update(k - 1, n, 1, 0, 0, low.n);
      }
    } else {
      long long num = high.query(k, k + 1, 0, 0, high.n);
      if (num == n - k)
        score = l;
      else {
        score = r;
        high.update(0, k, 1, 0, 0, high.n);
      }
    }
    ans += score;
  }
  cout << ans << endl;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
