#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i,n) for(int i = 0; i < (int) n; ++i)
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;
typedef long long ll;

void solve() {
  int n;
  cin >> n;

  vector<vector<int>> x(n, vector<int>(3));
  forn(i, n) forn(j, 3) cin >> x[i][j];

  sort(all(x), [&](const vector<int>& lhs, const vector<int>& rhs) {
    return lhs[1] - lhs[2] > rhs[1] - rhs[2]; // sort by L-R
  });

  set<int> left;
  forn(i,n) left.insert(i+1);

  int first = n, skip = 0;
  ll ans = 0;

  forn(i, n) {
    if (x[i][1] - x[i][2] < 0) {
      first = i;
      break;
    }

    // greatest e in left <= x[i][0]
    auto it = left.upper_bound(x[i][0]);
    if (it == left.begin()){
      ans += x[i][2];
      skip++;
      continue;
    }
    --it;

    left.erase(it);
    ans += x[i][1];
  }

  left.clear();
  for(int i = n - 1; i >= first; i--)
    left.insert(i+1);

  for(int i = n - 1; i >= first; i--) {
    assert(x[i][1] - x[i][2] < 0);

    // want to be in smallest e > x[i][0]
    auto it = left.upper_bound(x[i][0]);

    if (it == left.end()){
      ans += x[i][1];
      left.erase(left.begin());
      continue;
    }

    left.erase(it);
    ans += x[i][2];
  }

  cout << ans << "\n";
}

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