#include <bits/stdc++.h>

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<pair<int,int>> pos, neg;
  long long sum = 0;

  for (int i = 0; i < n; i++) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l > r) {
      pos.emplace_back(k, l - r);
    } else {
      neg.emplace_back(n - k, r - l);
    }
    sum += min(l, r);
  }
  
  {
    sort(pos.begin(), pos.end());
    multiset<int> q;
    int k, v;
    for (auto c : pos) {
      tie(k, v) = c;
      q.insert(v);
      if (q.size() > k) q.erase(q.begin());
    }
    for (auto v : q) sum += v;
  }
  {
    sort(neg.begin(), neg.end());
    multiset<int> q;
    int k, v;
    for (auto c : neg) {
      tie(k, v) = c;
      q.insert(v);
      if (q.size() > k) q.erase(q.begin());
    }
    for (auto v : q) sum += v;
  }

  cout << sum << '\n';
  return;
}

int main() {
  ios_base::sync_with_stdio(false);

  int tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
