#include <bits/stdc++.h>
namespace {
static const int N_MAX = 100000 * 2;
static int m_K[N_MAX];
static int m_L[N_MAX];
static int m_R[N_MAX];
static int m_rank[N_MAX];
}  // namespace
namespace NS20200711Acing {
class CamelTrain final {
 public:
  CamelTrain() = default;
  ~CamelTrain() = default;

 public:
  void Run() {
    std::cin >> m_T;
    for (int x = 0; x < m_T; ++x) {
      int N = 0;
      std::cin >> N;
      for (int y = 0; y < N; ++y) {
        std::cin >> m_K[y];
        std::cin >> m_L[y];
        std::cin >> m_R[y];
      }
      int sum = 0;
      std::vector<int> upper_rakuda = {};
      std::vector<int> lower_rakuda = {};
      for (int i = 0; i < N; ++i) {
        sum += (m_L[i] > m_R[i]) ? m_R[i] : m_L[i];
        if (m_L[i] < m_R[i])
          lower_rakuda.push_back(i);
        else
          upper_rakuda.push_back(i);
      }
      {
        auto compare = [&](int a, int b) {
          return (m_L[a] - m_R[a]) < (m_L[b] - m_R[b]);
        };
        std::priority_queue<int, std::vector<int>, decltype(compare)> rakuda{
            compare};
        for (int i = 0; i < N; ++i) {
          std::priority_queue<int, std::vector<int>, decltype(compare)> tmp{
              compare};
          for (int j = 0; j < upper_rakuda.size(); ++j) {
            if (m_K[upper_rakuda[j]] == i + 1) {
              tmp.push(upper_rakuda[j]);
            }
          }
          while (!tmp.empty() && rakuda.size() < i + 1) {
            rakuda.push(tmp.top());
            tmp.pop();
          }
        }
        while (rakuda.size() > 0) {
          auto r = rakuda.top();
          sum += m_L[r] - m_R[r];
          rakuda.pop();
        }
      }
      {
        auto compare = [&](int a, int b) {
          return (m_R[a] - m_L[a]) < (m_R[b] - m_L[b]);
        };
        std::priority_queue<int, std::vector<int>, decltype(compare)> rakuda{
            compare};
        for (int i = N - 1; i >= 0; --i) {
          std::priority_queue<int, std::vector<int>, decltype(compare)> tmp{
              compare};
          for (int j = 0; j < lower_rakuda.size(); ++j) {
            if (m_K[lower_rakuda[j]] == i) {
              tmp.push(lower_rakuda[j]);
            }
          }
          while (!tmp.empty() && rakuda.size() < N - i) {
            rakuda.push(tmp.top());
            tmp.pop();
          }
        }
        while (rakuda.size() > 0) {
          auto r = rakuda.top();
          sum += m_R[r] - m_L[r];
          rakuda.pop();
        }
      }
      std::cout << sum;
    }
  }

 private:
  int m_T;
};
}  // namespace NS20200711Acing
int main() {
  NS20200711Acing::CamelTrain prob;
  prob.Run();
  return 0;
}
