#include <bits/stdc++.h>
using namespace std;
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    auto cmp = [](const pair<int, pair<int, int> > &A,
                  const pair<int, pair<int, int> > &B) -> bool {
      if (A.first == B.first) {
        return A.second > B.second;
      }
      return A.first < B.first;
    };
    priority_queue<pair<int, pair<int, int> >,
                   vector<pair<int, pair<int, int> > >, decltype(cmp)>
        heap(cmp);
    for (int i = 1; i <= n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      heap.push({l - r, {k, r}});
    }
    long long best = 0;
    int taken = 0;
    queue<pair<int, pair<int, int> > > aux;
    while (!heap.empty()) {
      if (!aux.empty()) {
        heap.push(aux.front());
        aux.pop();
      }
      if (taken < heap.top().second.first) {
        int allowance = 1;
        if (aux.empty()) {
          while (!heap.empty() and heap.top().first > 0 and
                 taken + allowance < heap.top().second.first) {
            allowance += 1;
            aux.push(heap.top());
            heap.pop();
          }
        }
        taken += 1;
        if (heap.empty()) {
          heap.push(aux.front());
          aux.pop();
        }
        if (heap.top().first < 0 and !aux.empty()) {
          heap.push(aux.front());
          aux.pop();
        }
        best = best + heap.top().first + heap.top().second.second;
        heap.pop();
      } else {
        best = best + heap.top().second.second;
        heap.pop();
      }
    }
    cout << best << '\n';
  }
  return 0;
}
