#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n;
  int64_t k, l, r, b;
  pair<int64_t, int64_t> a;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    int64_t ans = 0, ln = 0;
    priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                   greater<pair<int64_t, int64_t>>>
        lq, rq;
    for (int j = 0; j < n; j++) {
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l >= r) {
        lq.push({k, l - r});
        ln++;
      } else
        rq.push({k, r - l});
    }
    priority_queue<int64_t, vector<int64_t>, greater<int64_t>> la, ra;
    int64_t lac = 0, rac = 0;
    for (int j = 1; j <= ln; j++) {
      if (lq.empty()) break;
      a = lq.top();
      while (a.first == j) {
        lq.pop();
        la.push(a.second);
        lac++;
        ans += a.second;
        if (lq.empty())
          break;
        else
          a = lq.top();
      }
      while (lac > j) {
        b = la.top();
        la.pop();
        ans -= b;
        lac--;
      }
    }
    while (!lq.empty()) {
      a = lq.top();
      ans += a.second;
      lq.pop();
    }
    for (int j = n - 1; j > ln; j--) {
      if (rq.empty()) break;
      a = rq.top();
      while (a.first == j) {
        rq.pop();
        ra.push(a.second);
        rac++;
        ans += a.second;
        if (rq.empty())
          break;
        else
          a = rq.top();
      }
      while (rac > n - j) {
        b = ra.top();
        ra.pop();
        ans -= b;
        rac--;
      }
    }
    while (!rq.empty()) {
      a = rq.top();
      if (a.first != n) ans += a.second;
      rq.pop();
    }
    cout << ans << endl;
  }
}
