#include <bits/stdc++.h>
struct Camel {
  int L, R, K, diff;
};
void main_impl() {
  int N;
  std::cin >> N;
  int64_t result{0};
  std::vector<Camel> L_family, R_family;
  L_family.reserve(N);
  R_family.reserve(N);
  for (auto i = 0; i < N; i++) {
    Camel camel;
    std::cin >> camel.K >> camel.L >> camel.R;
    if (camel.L == camel.R) {
      result += camel.L;
    } else if (camel.L > camel.R) {
      camel.diff = camel.L - camel.R;
      L_family.push_back(std::move(camel));
    } else {
      camel.diff = camel.R - camel.L;
      R_family.push_back(std::move(camel));
    }
  }
  std::sort(
      L_family.begin(), L_family.end(),
      [](Camel const& left, Camel const& right) { return left.K >= right.K; });
  std::sort(
      R_family.begin(), R_family.end(),
      [](Camel const& left, Camel const& right) { return left.K <= right.K; });
  auto compare = [](Camel const& left, Camel const& right) {
    return left.diff < right.diff;
  };
  std::priority_queue<Camel, std::vector<Camel>, decltype(compare)> in_line{
      compare};
  {
    auto iter = L_family.begin();
    for (auto i = static_cast<int>(L_family.size()); i >= 1; i--) {
      for (; iter != L_family.end() && i <= iter->K; iter++) {
        in_line.push(*iter);
      }
      if (!in_line.empty()) {
        result += in_line.top().L;
        in_line.pop();
      } else {
      }
    }
    while (!in_line.empty()) {
      result += in_line.top().R;
      in_line.pop();
    }
  }
  {
    auto iter = R_family.begin();
    for (auto i = N - static_cast<int>(R_family.size()) + 1; i <= N; i++) {
      for (; iter != R_family.end() && i > iter->K; iter++) {
        in_line.push(*iter);
      }
      if (!in_line.empty()) {
        result += in_line.top().R;
        in_line.pop();
      } else {
      }
    }
    while (!in_line.empty()) {
      result += in_line.top().L;
      in_line.pop();
    }
  }
  std::cout << result << std::endl;
}
int main() {
  int T;
  std::cin >> T;
  for (auto i = 0; i < T; i++) {
    main_impl();
  }
  return 0;
}
