#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = (ll)1e18;
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
void p() { std::cout << '\n'; }
template <typename Head, typename... Tail>
void p(Head head, Tail... tail) {
  std::cout << head << (sizeof...(tail) ? " " : "");
  p(tail...);
}
template <typename T>
inline void pv(std::vector<T>& v) {
  for (ll i = 0, N = v.size(); i < N; i++)
    std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T>
inline bool chmax(T& a, T b) {
  return a < b && (a = b, true);
}
template <typename T>
inline bool chmin(T& a, T b) {
  return a > b && (a = b, true);
}
template <typename T>
inline void uniq(std::vector<T>& v) {
  v.erase(std::unique(v.begin(), v.end()), v.end());
}
template <typename T>
class BinaryIndexedTree {
 public:
  class BinaryIndexedTreeNaive {
   public:
    ll size;
    vector<T> data;
    BinaryIndexedTreeNaive(ll N) : size(N + 1), data(size, 0) {}
    T sum(ll i) {
      T ret = 0;
      for (--i; i >= 0; i = (i & (i + 1)) - 1) ret += data[i];
      return ret;
    }
    T sum(ll i, ll j) { return sum(j) - sum(i); }
    void add(ll i, T x) {
      for (; i < size; i |= i + 1) data[i] += x;
    }
  };
  ll size;
  BinaryIndexedTreeNaive b, c;
  BinaryIndexedTree(ll N) : size(N), b(size), c(size) {}
  void add(ll i, ll j, T x) {
    b.add(i, x * -i);
    b.add(j, x * j);
    c.add(i, x);
    c.add(j, x * -1);
  }
  T sum(ll i) { return b.sum(i + 1) + c.sum(i + 1) * i; }
  T sum(ll i, ll j) { return sum(j) - sum(i); }
};
template <typename T>
std::ostream& operator<<(std::ostream& s, BinaryIndexedTree<T>& bit) {
  for (ll i = 0; i < bit.size; i++)
    s << bit.sum(i, i + 1) << "\t\n"[i == bit.size - 1];
  return s;
}
constexpr ll T_MAX = 1e5;
constexpr ll N_MAX = 2 * 1e5;
constexpr ll L_I_MAX = 1e9;
constexpr ll R_I_MAX = 1e9;
ll T;
void solve() {
  cin >> T;
  vector<ll> anslist(T);
  auto f = [&](vector<array<ll, 3>>& a) -> ll {
    ll N = a.size();
    sort(a.begin(), a.end(), [](const array<ll, 3>& x, const array<ll, 3>& y) {
      ll xa = x[1] - x[2], ya = y[1] - y[2];
      if (xa != ya) return xa > ya;
      return x[0] < y[0];
    });
    ll ans = 0;
    BinaryIndexedTree<ll> bit(N);
    for (ll i = 0, i_length = (N); i < i_length; ++i) {
      ll index = a[i][0] + 1;
      if (index > N) index = N;
      ll t = bit.sum(0, index);
      if (t < a[i][0] + 1) {
        ans += a[i][1];
      } else {
        ans += a[i][2];
      }
      bit.add(a[i][0], a[i][0] + 1, 1);
    }
    return ans;
  };
  for (ll q = 0, q_length = (T); q < q_length; ++q) {
    ll N;
    cin >> N;
    vector<array<ll, 3>> a;
    vector<array<ll, 3>> b;
    ll ans = 0;
    for (ll i = 0, i_length = (N); i < i_length; ++i) {
      ll a0, a1, a2;
      cin >> a0 >> a1 >> a2;
      a0--;
      if (a1 == a2 || a0 == N - 1) {
        ans += a1;
        continue;
      }
      if (a1 > a2)
        a.push_back({a0, a1, a2});
      else
        b.push_back({N - 1 - a0, a2, a1});
    }
    ll X = f(a);
    ll Y = f(b);
    do {
    } while (false);
    anslist[q] = X + Y + ans;
  }
  for (ll i = 0, i_length = (anslist.size()); i < i_length; ++i) p(anslist[i]);
}
signed main() {
  solve();
  return 0;
}
