#include <bits/stdc++.h>
using namespace std;
vector<int> diff1[200001], diff2[200002];
void solve() {
  for (int i = 0; i < (200001); i++) diff1[i].clear();
  for (int i = 0; i < (200002); i++) diff2[i].clear();
  int N;
  cin >> N;
  long long ans = 0;
  for (int i = 0; i < (N); i++) {
    int K, L, R;
    cin >> K >> L >> R;
    if (L >= R)
      diff1[K].push_back(L - R);
    else
      diff2[K + 1].push_back(R - L);
    ans += min(L, R);
  }
  priority_queue<int, vector<int>, greater<int>> que;
  for (int i = (1); i < (N + 1); i++) {
    for (int d : diff1[i]) que.push(d);
    while (que.size() > i) que.pop();
  }
  while (!que.empty()) {
    ans += que.top();
    que.pop();
  }
  for (int i = (1); i < (N + 1); i++) {
    for (int d : diff2[N + 1 - i]) que.push(d);
    while (que.size() > i) que.pop();
  }
  while (!que.empty()) {
    ans += que.top();
    que.pop();
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  for (int i = 0; i < (T); i++) solve();
  return 0;
}
