#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9+7;
constexpr llong inf = mod * mod;

int main() {
  llong T;
  cin >> T;
  while (T--) {
    llong N;
    vector<vector<lpair>> l, r;
    cin >> N;
    l.resize(N + 1);
    r.resize(N + 1);
    for (int i = 0; i < N; i++) {
      llong K, L, R;
      cin >> K >> L >> R;
      if (K < N && L < R) {
        r[K].push_back(lpair(L, R));
      }
      else {
        l[K].push_back(lpair(L, R));
      }
    }

    llong ans = 0;
    priority_queue<llong> left, right;
    for (int i = 1; i <= N; i++) {
      for (auto vv: l[i]) {
        ans += vv.first;
        left.push(vv.second - vv.first);
      }
      while (left.size() > i) {
        ans += left.top();
        left.pop();
      }
    }
    for (int i = N - 1; i >= 1; i--) {
      for (auto vv: r[i]) {
        ans += vv.second;
        right.push(vv.first - vv.second);
      }
      while (right.size() > N - i) {
        ans += right.top();
        right.pop();
      }
    }
    cout << ans << endl;
  }


  return 0;
}
