#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long ans = 0;
  vector<long long> k(n), l(n), r(n), m(n);
  vector<vector<long long>> L(n + 1), R(n + 1);
  for (long long i = 0; i < n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    long long mini = min(l[i], r[i]);
    ans += mini;
    l[i] -= mini;
    r[i] -= mini;
    m[i] = max(l[i], r[i]);
    if (l[i] > r[i]) {
      L[k[i]].push_back(i);
    } else if (l[i] < r[i]) {
      R[k[i]].push_back(i);
    }
  }
  priority_queue<pair<long long, long long>> pq;
  long long now = 1;
  for (long long i = 1; i < n + 1; i++) {
    for (long long x : L[i]) {
      pq.push({m[x], i});
    }
    while (!pq.empty()) {
      pair<long long, long long> p = pq.top();
      pq.pop();
      if (p.second >= now) {
        ans += p.first;
        now++;
      }
    }
  }
  now = n;
  for (long long i = n + 1 - 1; i >= 1; i--) {
    for (long long x : R[i]) {
      pq.push({m[x], i});
    }
    while (!pq.empty()) {
      pair<long long, long long> p = pq.top();
      pq.pop();
      if (p.second < now) {
        ans += p.first;
        now--;
        break;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
