#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());
}
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 {
    sort((a).begin(), (a).end());
    ll ans = 0;
    multiset<array<ll, 4>> q;
    for (ll i = 0, i_length = (a.size()); i < i_length; ++i) {
      if (a[i][0] >= (ll)q.size()) {
        ans += a[i][1];
        q.insert({(a[i][1] - a[i][2]), a[i][0], a[i][1], a[i][2]});
        continue;
      }
      while (!(a[i][0] >= (ll)q.size())) {
        auto it = q.begin();
        if ((*it)[0] >= a[i][0]) break;
        ans -= (*it)[2];
        ans += (*it)[3];
        q.erase(it);
      }
      if (a[i][0] >= q.size()) {
        ans += a[i][1];
        q.insert({(a[i][1] - a[i][2]), a[i][0], a[i][1], a[i][2]});
      }
    }
    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;
    vector<array<ll, 3>> W(N);
    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 - 2 - 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;
}
