#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

void solve() {
  int n; cin >> n;
  vector<int> k(n), l(n), r(n);
  vector<vector<int>> diff(n);
  ll ans = 0;
  REP(i, n) {
    cin >> k[i] >> l[i] >> r[i];
    ans += r[i];
    if (l[i] > r[i]) diff[k[i] - 1].emplace_back(i);
  }
  int pos = 0;
  vector used(n, false);
  priority_queue<pair<int, int>> que;
  for (int i = n - 1; i >= 0; --i) {
    for (int e : diff[i]) que.emplace(l[e] - r[e], e);
    if (!que.empty()) {
      ans += que.top().first;
      int idx = que.top().second; que.pop();
      ++pos;
      used[idx] = true;
    }
  }
  REP(i, n) {
    if (!used[i] && l[i] >= r[i]) {
      ++pos;
      used[i] = true;
    }
  }
  vector<vector<int>> era(n);
  multiset<int> st;
  REP(i, n) {
    if (!used[i]) {
      if (k[i] < n) era[k[i]].emplace_back(l[i] - r[i]);
      st.emplace(l[i] - r[i]);
    }
  }
  int cnt = 0;
  REP(i, pos) {
    cnt += era[i].size();
    for (int e : era[i]) st.erase(st.lower_bound(e));
  }
  for (; pos < n; ++pos) {
    for (int e : era[pos]) {
      auto it = st.lower_bound(e);
      if (it != st.end() && *it == e) {
        ++cnt;
        st.erase(it);
      }
    }
    if (cnt == 0) {
      assert(!st.empty());
      ans += *st.rbegin();
      st.erase(prev(st.end()));
    } else {
      --cnt;
    }
  }
  cout << ans << '\n';
}

int main() {
  int t; cin >> t;
  while (t--) solve();
  return 0;
}
