#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
constexpr int INF = 2e9;
typedef tuple<int,int,int> TP;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    ll res = 0;
    vector<vector<int>> left(n), right(n);
    rep(i,n) {
      int x, l, r;
      cin >> x >> l >> r;
      x--;
      res += min(l, r);
      if (l - r < 0) right[x].emplace_back(r - l);
      else left[x].emplace_back(l - r);
    }
    priority_queue<int, vector<int>, greater<int>> pq;

    for (int i = 0; i < n; i++) {
      for (auto v : left[i]) pq.push(v);
      while (pq.size() > (i + 1)) {
        pq.pop();
      }
    }
    while (!pq.empty()) res += pq.top(), pq.pop();

    for (int i = n - 1; i >= 0; i--) {
      for (auto v : right[i]) pq.push(v);
      while (pq.size() > (n - i - 1)) {
        pq.pop();
      }
    }
    while (!pq.empty()) res += pq.top(), pq.pop();

    cout << res << "\n";
  }
  return 0;
} 