#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void CHMAX(T& a, const T b) {
  if (a < b) a = b;
}
template <typename T>
inline void CHMIN(T& a, const T b) {
  if (a > b) a = b;
}
constexpr long long MOD = 1000000007ll;
const double EPS = 1e-9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, k, l, r;
    cin >> n;
    vector<pair<int, int> > a, b;
    long long ans = 0;
    for (int i = 0; i < (n); ++i) {
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l > r) {
        a.push_back(pair<int, int>(k, l - r));
      }
      if (l < r) {
        b.push_back(pair<int, int>(n - k, r - l));
      }
    }
    sort((a).begin(), (a).end());
    sort((b).begin(), (b).end());
    priority_queue<int> pq;
    for (auto xy : a) {
      int x = xy.first, y = xy.second;
      ans += y;
      pq.push(y);
      if (pq.size() > x) {
        ans -= pq.top();
        pq.pop();
      }
    }
    priority_queue<int> pq2;
    for (auto xy : b) {
      int x = xy.first, y = xy.second;
      ans += y;
      pq2.push(y);
      if (pq2.size() > x) {
        ans -= pq2.top();
        pq2.pop();
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
