#include <bits/stdc++.h>
using namespace std;
int N;
int K[200000];
long long L[200000], R[200000];
void solve() {
  cin >> N;
  long long ans = 0;
  vector<pair<long long, long long> > vl, vr;
  for (int i = 0; i < N; i++) {
    cin >> K[i] >> L[i] >> R[i];
    ans += min(L[i], R[i]);
    K[i]--;
    long long m = min(L[i], R[i]);
    L[i] -= m;
    R[i] -= m;
    if (L[i] > 0) {
      vl.push_back(pair<long long, long long>(K[i], L[i]));
    }
    if (R[i] > 0) {
      int rem = N - K[i] - 2;
      vr.push_back(pair<long long, long long>(rem, R[i]));
    }
  }
  sort(vl.begin(), vl.end());
  sort(vr.begin(), vr.end());
  priority_queue<long long> que;
  for (pair<long long, long long> p : vl) {
    long long cur = p.second;
    int idx = p.first;
    if (que.size() <= idx) {
      que.push(cur);
    } else {
      long long top = que.top();
      if (top < cur) {
        que.pop();
        que.push(cur);
      }
    }
  }
  while (!que.empty()) {
    long long top = que.top();
    ans += top;
    que.pop();
  }
  for (pair<long long, long long> p : vr) {
    long long cur = p.second;
    int idx = p.first;
    if (idx == -1) continue;
    if (que.size() <= idx) {
      que.push(cur);
    } else {
      long long top = que.top();
      if (top < cur) {
        que.pop();
        que.push(cur);
      }
    }
  }
  while (!que.empty()) {
    long long top = que.top();
    ans += top;
    que.pop();
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
