#line 1 "main.cpp"

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <queue>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

i64 calc(const std::vector<std::vector<i32>> &vec) {
  i64 res = 0;
  std::priority_queue<i32> que;
  for (auto i: revrange(0, vec.size())) {
    for (auto x: vec[i]) {
      que.push(x);
    }
    if (!que.empty()) {
      res += que.top();
      que.pop();
    }
  }
  return res;
}

int main() {
  size_t testcases;
  std::cin >> testcases;
  auto solve = [&]() -> bool {
    size_t N;
    std::cin >> N;
    std::vector<std::tuple<i32, i32, i32>> A(N);
    for (auto &[k, l, r]: A) {
      std::cin >> k >> l >> r;
    }
    i32 left = 0, right = 0;
    for (const auto [k, l, r]: A) {
      if (l < r) ++right;
      if (l > r) ++left;
    }
    i64 base = 0;
    std::vector<std::vector<i32>> L(left + 1);
    std::vector<std::vector<i32>> R(right + 1);
    for (const auto [k, l, r]: A) {
      if (l < r) {
        base += l;
        if (k == N) continue;
        R[std::min<i32>(right, N - k + 1)].push_back(r - l);
      }
      else if (l > r) {
        base += r;
        L[std::min<i32>(left, k - 1)].push_back(l - r);
      }
      else {
        base += l;
      }
    }
    std::cout << base + calc(L) + calc(R) << '\n';
    return 0;
  };
  while (testcases--) {
    solve();
  }
  return 0;
}

/**
 * @title Template
 */
