#include <bits/stdc++.h>
using namespace std;
void solve() {
  int N;
  cin >> N;
  vector<vector<pair<long long, int>>> X(N);
  long long ans = 0;
  set<pair<long long, int>> S;
  for (int i = 0; i < N; ++i) {
    long long K, L, R;
    cin >> K >> L >> R;
    ans += R;
    X[K - 1].emplace_back(L - R, i);
  }
  set<pair<long long, int>> unused;
  int cnt = 0;
  for (int i = N - 1; i >= 0; --i) {
    for (auto e : X[i]) {
      S.insert(e);
    }
    if (S.empty()) {
      continue;
    }
    auto p = *S.rbegin();
    S.erase(p);
    if (p.first < 0) {
      unused.emplace(p);
      continue;
    }
    ans += p.first;
    ++cnt;
  }
  for (auto e : S) unused.emplace(e);
  for (int i = 0; i < cnt; ++i) {
    for (auto e : X[i]) {
      if (!unused.count(e)) continue;
      unused.erase(e);
      if (e.first < 0) unused.emplace(0, e.second);
    }
  }
  for (int i = cnt; i < N; ++i) {
    auto p = *unused.rbegin();
    ans += p.first;
    unused.erase(p);
    for (auto e : X[i]) {
      if (e.first < 0 and unused.count(e)) {
        unused.erase(e);
        unused.emplace(0, e.second);
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
}
