#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
typedef tuple<LL, LL, LL> P;

LL solve() {
  int N; cin >> N;

  LL ans = 0;
  map<int, vector<LL>> ml, mr;
  for (int i = 0; i < N; ++i) {
    LL K, L, R; cin >> K >> L >> R;
    if (L >= R) {
      ans += R;
      ml[K].push_back(L - R);
    } else {
      ans += L;
      mr[K].push_back(R - L);
    }
  }

  priority_queue<LL, vector<LL>, greater<LL>> quel, quer;
  for (int i = 1; i <= N; ++i) {
    for (LL v : ml[i]) {
      quel.push(v);
    }
    while (quel.size() > i) quel.pop();

    for (LL v : mr[N - i]) {
      quer.push(v);
    }
    while (quer.size() > i) quer.pop();
  }

  while (!quel.empty()) {
    ans += quel.top(); quel.pop();
  }
  while (!quer.empty()) {
    ans += quer.top(); quer.pop();
  }

  return ans;
}

int main() {
  int T; cin >> T;
  for (int i = 0; i < T; ++i) {
    cout << solve() << endl;
  }
}
