#include <bits/stdc++.h>
using namespace std;
template <typename T>
using V = vector<T>;
using VL = V<long long>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<long long, long long>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline long long len(V<T> arr) {
  return arr.size();
}
struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};
template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}
const long long INF = 1e18;
void solve();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  try {
    solve();
  } catch (exit_exception& e) {
  }
  return 0;
}
void solve() {
  long long T;
  cin >> T;
  auto solve_case = []() {
    long long N;
    cin >> N;
    struct Camel {
      long long K, diff, min;
    };
    struct CompareCamel {
      bool operator()(Camel const& lhs, Camel const& rhs) {
        return lhs.diff < rhs.diff;
      }
    };
    V<Camel> front(0);
    V<Camel> back(0);
    for (long long i = 0; i < (N); ++i) {
      long long K, L, R;
      cin >> K >> L >> R;
      if (L >= R) {
        front.push_back({K, L - R, min(L, R)});
      } else {
        back.push_back({N - K, R - L, min(L, R)});
      }
    }
    auto solve_partial = [&](V<Camel>& camels) -> long long {
      long long res = 0;
      for (auto& c : camels) res += c.min;
      map<long long, V<Camel>> k_to_camel;
      for (auto& c : camels) {
        long long k = min(c.K, (long long)camels.size());
        k_to_camel[k].push_back(c);
      }
      priority_queue<Camel, V<Camel>, CompareCamel> q;
      for (long long i = 1; i <= (camels.size()); ++i) {
        for (auto& c : k_to_camel[i]) q.push(c);
        while (q.size() > i) q.pop();
      }
      while (q.size()) {
        auto c = q.top();
        res += c.diff;
        q.pop();
      }
      return res;
    };
    long long res = solve_partial(front) + solve_partial(back);
    cout << res << "\n";
  };
  for (long long i = 0; i < (T); ++i) solve_case();
}
