#include <bits/stdc++.h>
using namespace std;
struct C {
  int k, l, r, diff;
};
int cmp(C &a, C &b) { return a.k < b.k; }
void go() {
  int n;
  cin >> n;
  int cntA = 0, cntB = 0;
  vector<C> c1, c2;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    C t;
    cin >> t.k >> t.l >> t.r;
    t.k--;
    t.diff = t.l - t.r;
    if (t.diff > 0)
      c1.push_back(t);
    else
      c2.push_back(t);
    ans += min(t.l, t.r);
  }
  sort(c1.begin(), c1.end(), cmp);
  sort(c2.begin(), c2.end(), cmp);
  reverse(c2.begin(), c2.end());
  priority_queue<int, vector<int>, greater<int>> pq1;
  int p = 0;
  for (int i = 0; i < n; i++) {
    while (p < c1.size() && c1[p].k <= i) {
      pq1.push(c1[p].diff);
      ans += c1[p].diff;
      p++;
    }
    while (pq1.size() > i + 1) {
      ans -= pq1.top();
      pq1.pop();
    }
  }
  priority_queue<int, vector<int>, greater<int>> pq2;
  int q = 0;
  while (c2[q].k == n - 1) q++;
  for (int i = 0; i < n; i++) {
    while (q < c2.size() && c2[q].k >= n - 1 - i) {
      pq2.push(-c2[q].diff);
      ans -= c2[q].diff;
      q++;
    }
    while (pq2.size() > i + 1) {
      ans -= pq2.top();
      pq2.pop();
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) go();
}
