#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const long long INF64 = 1LL << 60;
struct Camel {
  int k;
  long long l, r;
  bool operator<(const Camel &rhs) const {
    return (k == rhs.k) ? abs(l - r) > abs(rhs.l - rhs.r) : k < rhs.k;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long ans = 0;
    vector<Camel> left;
    vector<Camel> right;
    for (int i = 0; i < n; i++) {
      Camel foo;
      cin >> foo.k >> foo.l >> foo.r;
      if (foo.l >= foo.r)
        left.push_back(foo);
      else {
        foo.k = n - foo.k;
        right.push_back(foo);
      }
      ans += min(foo.l, foo.r);
    }
    sort(left.begin(), left.end());
    sort(right.begin(), right.end());
    priority_queue<long long, vector<long long>, greater<long long>> pq;
    for (int i = 0; i < left.size(); i++) {
      if (left[i].k >= i + 1) {
        ans += left[i].l - left[i].r;
        pq.push(left[i].l - left[i].r);
      } else if (left[i].l - left[i].r > pq.top()) {
        ans -= pq.top();
        pq.pop();
        pq.push(left[i].l - left[i].r);
        ans += pq.top();
      }
    }
    while (!pq.empty()) pq.pop();
    for (int i = 0; i < right.size(); i++) {
      if (right[i].k >= i + 1) {
        ans += right[i].r - right[i].l;
        pq.push(right[i].r - right[i].l);
      } else if (!pq.empty() && right[i].r - right[i].l > pq.top()) {
        ans -= pq.top();
        pq.pop();
        pq.push(right[i].r - right[i].l);
        ans += pq.top();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
