#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
  int t;
  cin >> t;
  while (t-- > 0) {
    int n;
    cin >> n;
    vector<int> K(n);
    vector<int> L(n);
    vector<int> R(n);
    for (int i = 0; i < n; ++i) {
      cin >> K[i] >> L[i] >> R[i];
    }
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    long long res = 0;
    for (int i = 0; i < n; ++i) {
      if (L[i] < R[i]) {
        res += L[i];
        A.emplace_back(n - K[i], R[i] - L[i]);
      } else if (L[i] > R[i]) {
        res += R[i];
        B.emplace_back(K[i], L[i] - R[i]);
      } else {
        res += L[i];
      }
    }
    auto calc = [&](vector<pair<int, int>>& A) {
      auto cmp = [&](const pair<int, int>& l, const pair<int, int>& r) {
        if (l.first == r.first) {
          return l.second > r.second;
        }
        return l.first < r.first;
      };
      sort(A.begin(), A.end(), cmp);
      priority_queue<int, vector<int>, greater<int>> pq;
      int res = 0;
      for (auto& p : A) {
        int k = p.first;
        int v = p.second;
        pq.push(v);
        while (pq.size() > k) {
          pq.pop();
        }
      }
      while (!pq.empty()) {
        res += pq.top();
        pq.pop();
      }
      return res;
    };
    res += calc(A);
    res += calc(B);
    cout << res << '\n';
  }
  return 0;
}
