#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long>;
using vvi = vector<vi>;
long long n;
void mysort2(vi &a, vi &b, long long flag) {
  if (a.size() > 0) {
    vector<pair<long long, long long>> p(a.size());
    for (long long i = 0; i < a.size(); i++) {
      p[i].first = a[i];
      p[i].second = b[i];
    }
    sort(p.begin(), p.end());
    if (flag == 1) {
      reverse(p.begin(), p.end());
    }
    for (long long i = 0; i < a.size(); i++) {
      a[i] = p[i].first;
      b[i] = p[i].second;
    }
  }
  return;
}
int main() {
  long long ans = 0;
  long long t;
  cin >> t;
  for (long long tt = 0; tt < t; tt++) {
    priority_queue<long long, vector<long long>, greater<long long>> que;
    priority_queue<long long, vector<long long>, greater<long long>> que2;
    ans = 0;
    cin >> n;
    vi K(n), L(n), R(n);
    long long lnum = 0;
    long long rnum = 0;
    vi lp, rp;
    vi lk, rk;
    for (long long i = 0; i < n; i++) {
      cin >> K[i] >> L[i] >> R[i];
      if (L[i] < R[i]) {
        rnum++;
        long long tmp = (R[i] - L[i]) * 1000000001LL;
        rp.push_back(R[i] + tmp);
        rk.push_back(K[i]);
      } else {
        lnum++;
        long long tmp = (L[i] - R[i]) * 1000000001LL;
        lp.push_back(L[i] + tmp);
        lk.push_back(K[i]);
      }
    }
    mysort2(lk, lp, 0);
    for (long long i = 0; i < lnum; i++) {
      if (i < lk[i]) {
        ans += lp[i] % 1000000001LL;
        que.push(lp[i] / 1000000001LL);
      } else {
        long long tmp = lp[i] / 1000000001LL;
        if (que.top() < tmp) {
          ans -= que.top();
          ans += lp[i] % 1000000001LL;
          que.pop();
          que.push(tmp);
        } else {
          ans += (lp[i] % 1000000001LL) - lp[i] / 1000000001LL;
        }
      }
    }
    mysort2(rk, rp, 1);
    for (long long i = 0; i < rnum; i++) {
      if (i < n - rk[i]) {
        ans += rp[i] % 1000000001LL;
        que2.push(rp[i] / 1000000001LL);
      } else {
        long long tmp = rp[i] / 1000000001LL;
        if (que2.empty()) {
          ans += (rp[i] % 1000000001LL) - rp[i] / 1000000001LL;
        } else {
          if (que2.top() < tmp) {
            ans -= que2.top();
            ans += rp[i] % 1000000001LL;
            que2.pop();
            que2.push(tmp);
          } else {
            ans += (rp[i] % 1000000001LL) - rp[i] / 1000000001LL;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
