#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;
using llpair = pair<llong, lpair>;
constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;
int main() {
  llong T;
  cin >> T;
  while (T--) {
    llong N;
    vector<vector<lpair>> l, r;
    priority_queue<llong> lque, rque;
    cin >> N;
    l.resize(N + 1);
    r.resize(N + 1);
    for (int i = 0; i < N; ++i) {
      llong K, L, R;
      cin >> K >> L >> R;
      if (L > R) {
        l[K].push_back(lpair(L, R));
      } else {
        r[K].push_back(lpair(L, R));
      }
    }
    llong ans = 0;
    llong lid = 0, rid = 0;
    for (int i = 1; i <= N; i++) {
      for (auto vv : l[i]) {
        ans += vv.first;
        lque.push(vv.second - vv.first);
      }
      lid += l[i].size();
      for (int j = i + 1; j <= lid; j++) {
        ans += lque.top();
        lque.pop();
      }
    }
    for (int i = N; i >= 0; i--) {
      for (auto vv : r[i]) {
        ans += vv.second;
        rque.push(vv.first - vv.second);
      }
      rid += r[i].size();
      for (int j = N - i + 1; j <= rid; j++) {
        ans += rque.top();
        rque.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
