#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];
    }
    long long res = 0;
    for (int i = 0; i < n; ++i) {
      res += R[i];
    }
    vector<vector<int>> E(n);
    for (int i = 0; i < n; ++i) {
      int k = K[i] - 1;
      E[k].push_back(L[i] - R[i]);
    }
    for (auto& V : E) {
      sort(V.begin(), V.end());
    }
    priority_queue<int, vector<int>, greater<int>> pq;
    int req = 0;
    for (int i = n - 1; i >= 0; --i) {
      for (auto x : E[i]) {
        if (pq.size() < i + 1) {
          pq.push(x);
        }
      }
      while (pq.size() > i + 1) {
        int mx = pq.top();
        pq.pop();
        res += mx;
      }
    }
    while (!pq.empty()) {
      res += pq.top();
      pq.pop();
    }
    cout << res << '\n';
  }
  return 0;
}
