#include <bits/stdc++.h>
using namespace std;
long long h[200005][4];
int main() {
  int t;
  cin >> t;
  long long sum = 0;
  for (int w = 1; w <= t; w++) {
    sum = 0;
    priority_queue<long long, vector<long long>, greater<long long>> rem;
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long>>,
                   greater<pair<long long, long long>>>
        pque;
    long long n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      long long a, b, c;
      cin >> a >> b >> c;
      sum += c;
      pque.push(make_pair(a, b - c));
    }
    for (int i = 1; i <= n; i++) {
      pair<int, int> top = pque.top();
      long long ansa = top.first;
      long long ansb = top.second;
      pque.pop();
      if (ansa >= i) {
        sum += ansb;
        if (rem.size() != 0 && rem.top() < 0) {
          sum -= rem.top();
          rem.pop();
        }
        rem.push(ansb);
      } else {
        if (ansb > rem.top()) {
          sum += ansb;
          sum -= rem.top();
          rem.pop();
          rem.push(ansb);
        }
      }
    }
    cout << sum << endl;
  }
}
