#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int k[n], l[n], r[n];
    for (int i = 0; i < n; i++) cin >> k[i] >> l[i] >> r[i];
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long>>,
                   greater<pair<long long, long long>>>
        p1, p2;
    long long s = 0;
    vector<pair<long long, long long>> v1, v2;
    for (int i = 0; i < n; i++) {
      if (l[i] >= r[i]) {
        s = s + r[i];
        v1.push_back({k[i], l[i] - r[i]});
      }
    }
    sort(v1.begin(), v1.end());
    for (int i = 0; i < v1.size(); i++) {
      p1.push({v1[i].second, v1[i].first});
      if (p1.size() > v1[i].first) p1.pop();
    }
    while (!p1.empty()) {
      s = s + p1.top().first;
      p1.pop();
    }
    for (int i = 0; i < n; i++) {
      if (r[i] > l[i]) {
        s = s + l[i];
        v2.push_back({k[i], r[i] - l[i]});
      }
    }
    for (int i = 0; i < v2.size(); i++) {
      p2.push({v2[i].second, v2[i].first});
      if (p2.size() > n - v2[i].first) p2.pop();
    }
    while (!p2.empty()) {
      s = s + p2.top().first;
      p2.pop();
    }
    cout << s << endl;
  }
}
