#include <bits/stdc++.h>
using namespace std;
int n, ans;
void Solve(vector<pair<int, int>>& cams) {
  priority_queue<int> pq;
  int initalAns = ans;
  for (auto i : cams) {
    pq.push(-i.second);
    ans += i.second;
    while (pq.size() > i.first) ans += pq.top(), pq.pop();
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int testcases;
  cin >> testcases;
  while (testcases--) {
    cin >> n;
    ans = 0;
    std::vector<pair<int, int>> f;
    std::vector<pair<int, int>> b;
    for (int i = 1; i <= n; i++) {
      int K, L, R;
      cin >> K >> L >> R;
      if (L > R) {
        ans += R;
        f.push_back({K, L - R});
      } else {
        ans += L;
        b.push_back({n - K, R - L});
      }
    }
    sort(f.begin(), f.end());
    sort(b.begin(), b.end());
    Solve(f);
    Solve(b);
    std::cout << ans << "\n";
  }
  return 0;
}
