#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long T, n, k, l, r;
  cin >> T;
  vector<long long> ans(T, 0);
  for (long long t = 0; t < T; ++t) {
    cin >> n;
    map<long long, vector<long long> > A, B;
    for (long long i = 0; i < n; ++i) {
      cin >> k >> l >> r;
      ans[t] += min(l, r);
      if (l >= r)
        A[k].emplace_back(l - r);
      else
        B[k].emplace_back(r - l);
    }
    priority_queue<long long> que;
    for (long long j = 1; j <= n; ++j) {
      for (long long e : A[j]) que.push(e);
      while (que.size() > j) que.pop();
    }
    while (que.size()) {
      ans[t] += que.top();
      que.pop();
    }
    for (long long j = n - 1; j > 0; j--) {
      for (long long e : B[j]) que.push(e);
      while (que.size() > n - j) que.pop();
    }
    while (que.size()) {
      ans[t] += que.top();
      que.pop();
    }
    cout << (ans[t]) << endl;
  }
  return 0;
}
