#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B>
inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
void solve() {
  int N;
  cin >> N;
  vector<int> K(N), L(N), R(N);
  for (int i = (0); i < (N); ++i)
    scanf("%d %d %d", &K[i], &L[i], &R[i]), --K[i];
  vector<int> lv, rv;
  for (int i = (0); i < (N); ++i) {
    if (L[i] > R[i])
      lv.emplace_back(i);
    else
      rv.emplace_back(i);
  }
  auto calc = [&](vector<int> &lv) -> ll {
    ll res = 0;
    vector<int> ord(lv.size());
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord),
         [&](int i, int j) { return K[lv[i]] < K[lv[j]]; });
    set<pii> ss;
    for (int idx : ord) {
      int i = lv[idx];
      if ((int)ss.size() <= K[i]) {
        res += L[i];
        ss.emplace(L[i], i);
        continue;
      }
      if (!ss.empty() && L[i] - R[i] > ss.rend()->first) {
        int idx = ss.rend()->second;
        res += -L[idx] + R[idx];
        ss.erase(--ss.end());
        res += L[i];
        ss.emplace(L[i], i);
      } else
        res += R[i];
    }
    return res;
  };
  ll ans = calc(lv);
  for (int i : rv) {
    K[i] = N - (K[i] + 1) - 1;
    swap(L[i], R[i]);
  }
  ans += calc(rv);
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
